<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Parse::RecDescent - Generate Recursive-Descent Parsers - metacpan.org</title>
        <link rel="preload" as="fetch" href="https://metacpan.org/account/login_status" crossorigin="anonymous" />
        <link href="https://metacpan.org/_assets/b8ccceeed47a0652049703d99326a9cea4933443.css" rel="stylesheet" type="text/css">
        <script src="https://metacpan.org/_assets/6bfedafe2d7caa915b7d84f61b45936818e3242e.js" type="text/javascript" defer></script>
        <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of Parse-RecDescent - MetaCPAN" href="https://metacpan.org/dist/Parse-RecDescent/releases.rss" />
        <link rel="canonical" href="./Parse::RecDescent.html" />
        <meta name="description" content="Generate Recursive-Descent Parsers" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
        <link rel="shortcut icon" href="https://metacpan.org/static/icons/favicon.ico">
        <link rel="apple-touch-icon" sizes="152x152" href="https://metacpan.org/static/icons/apple-touch-icon.png">
        <link rel="search" href="https://metacpan.org/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN">
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

          (function(skey, ga_id){
            ga('create', ga_id, {
              siteSpeedSampleRate : 100,
              storage             : 'none',
              clientId            : localStorage.getItem(skey)
            });
            ga(function(tracker) {
              localStorage.setItem(skey, tracker.get('clientId'));
            });
            ga('send', 'pageview');
          })('ga:clientId', 'UA-27829474-1');
        </script>
<meta name="twitter:card"        content="summary" />
<meta name="twitter:url"         content="https://metacpan.org/pod/Parse::RecDescent" />
<meta name="twitter:title"       content="Parse::RecDescent" />
<meta name="twitter:description" content="Generate Recursive-Descent Parsers" />
<meta name="twitter:site"        content="metacpan" />
    </head>
    <body>
        <nav class="navbar navbar-default" role="navigation">
            <div class="header-logo-large hidden-xs">
              <a href="https://metacpan.org/" tabindex="0">
                <svg class="logo" aria-label="MetaCPAN">
                  <use class="logo" href="/static/images/metacpan-logo.svg#logo" />
                </svg>
              </a>
            </div>
            <div class="header-logo-icon visible-xs">
              <a href="https://metacpan.org/">
                <svg class="logo" aria-label="MetaCPAN">
                  <use class="logo" href="/static/images/metacpan-logo.svg#dots" />
                </svg>
              </a>
            </div>
            <ul class="nav navbar-nav menu-items hidden-xs hidden-sm">
              <li><a href="https://metacpan.org/about">About</a></li>
              <li><a href="https://metacpan.org/about/sponsors">Sponsor</a></li>
              <li><a href="https://grep.metacpan.org/">grep::cpan</a></li>
              <li><a href="https://metacpan.org/recent">Recent</a></li>
              <li><a href="https://metacpan.org/about/faq">FAQ</a></li>
              <li><a href="https://metacpan.org/tools">Tools</a></li>
              <li><a href="https://fastapi.metacpan.org/">API</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <button type="button" class="searchbar-btn visible-xs visible-sm">
                    <i class="fa fa-search button-fa-icon"></i>
                </button>
                <form action="https://metacpan.org/search" class="searchbar-form visible-md visible-lg search-form form-horizontal">
                   <input type="hidden" name="size" id="metacpan_search-size" value="20">
                  <div class="form-group">
                      <div class="search-group">
                        <i class="fa fa-search"></i>
                        <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value="">
                      </div>
                  </div>
                </form>
                    <li class="icon-slidepanel visible-xs visible-sm">
                      <button data-toggle="slidepanel" data-target=".slidepanel">
                        <span class="button-fa-icon">
                          <i class="fa fa-bars slidepanel-open"></i>
                          <i class="fa fa-times slidepanel-close"></i>
                        </span>
                      </button>
                    </li>
                <form action="https://metacpan.org/account/logout" method="POST" id="metacpan-logout"></form>
                <li class="dropdown logged_in" style="display: none;">
                    <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                      <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i>
                      <i class="fas fa-chevron-down"></i>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="https://metacpan.org/account/identities">Identities</a></li>
                        <li><a href="https://metacpan.org/account/profile">Profile</a></li>
                        <li><a href="https://metacpan.org/account/favorite/list">Favorites</a></li>
                        <li>
                            <a href="./Parse::RecDescent.html#" type="button" onclick="$('#metacpan-logout').submit(); return false">
                              Logout
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown logged_out" style="display: none;">
                    <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                      <i class="fa fa-user button-fa-icon" aria-hidden="true"></i>
                      <i class="fas fa-chevron-down"></i>
                    </button>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="https://metacpan.org/login/github">
                                <i class="fab fa-github fa-fw"></i>
                                GitHub
                            </a>
                        </li>
                        <li>
                            <a href="https://metacpan.org/login/twitter">
                                <i class="fab fa-twitter fa-fw"></i>
                                Twitter
                            </a>
                        </li>
                        <li>
                            <a href="https://metacpan.org/login/google">
                                <i class="fab fa-google fa-fw"></i>
                                Google
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown logged_placeholder">
                    <button>
                      <i class="fa fa-user button-fa-icon" aria-hidden="true"></i>
                    </button>
                </li>
            </ul>
        </nav>
        <div class="page-content ">
          <!--
          <div class="top-notify-banner">
            <i class="fas fa-info-circle"></i>
          </div>
          -->
          <nav class="sidebar">
            <div class="slidepanel">
              <ul class="nav-list ">
    <li class="nav-header no-margin-top">
      <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The date that this version of Parse-RecDescent was released.">
      <span class="relatize">04 Apr 2017 14:54:55 UTC</span>
    </li>
  <li>
    Distribution: <a href="https://metacpan.org/dist/Parse-RecDescent">Parse-RecDescent</a>
  </li>
  <li>
    Module version: 1.967015
  </li>
  <li>
    <a data-keyboard-shortcut="g s" href="https://metacpan.org/dist/Parse-RecDescent/source/lib/Parse/RecDescent.pm">Source</a>
    (<a href="https://metacpan.org/dist/Parse-RecDescent/source/lib/Parse/RecDescent.pm?raw=1">raw</a>)
  </li>
  <li>
    <a data-keyboard-shortcut="g b" href="https://metacpan.org/dist/Parse-RecDescent/source/lib/Parse">Browse</a>
    (<a href="https://metacpan.org/dist/Parse-RecDescent/source/lib/Parse?raw=1">raw</a>)
  </li>
    <li>
      <a data-keyboard-shortcut="g c" href="https://metacpan.org/dist/Parse-RecDescent/changes">Changes</a>
    </li>
    <li>
      <a class="nopopup" href="https://metacpan.org/dist/Parse-RecDescent/contribute">How to Contribute</a>
    </li>
    <li>
        <a rel="noopener nofollow" data-keyboard-shortcut="g r" href="https://github.com/jtbraun/Parse-RecDescent">Repository</a>
    </li>
    <li>
      <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.cpan.org/Dist/Display.html?Status=Active&amp;Queue=Parse-RecDescent">Issues</a>
      (15)
    </li>
    <li>
      <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=Parse-RecDescent+1.967015" title="Matrix">Testers</a>
        <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/P/Parse-RecDescent.html?oncpan=1&amp;distmat=1&amp;version=1.967015&amp;grade=2" style="color: #090">1280</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/P/Parse-RecDescent.html?oncpan=1&amp;distmat=1&amp;version=1.967015&amp;grade=3" style="color: #900">0</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/P/Parse-RecDescent.html?oncpan=1&amp;distmat=1&amp;version=1.967015&amp;grade=4">0</a>)</span>
    </li>
    <li>
      <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/JTBRAUN/Parse-RecDescent-1.967015">Kwalitee</a>
    </li>
    <li>
      <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on Parse-RecDescent who have released something to CPAN in the last 2 years (i.e. the # people likely able to release critical fixes in a timely manner)">
      Bus factor: 1
      </div>
    </li>
    <li>
      <a rel="noopener nofollow" href="http://cpancover.com/latest/Parse-RecDescent-1.967015/index.html">67.56% Coverage </a>
    </li>
    <li>
      License: perl_5
    </li>
    <li class="nav-header">Activity</li>
    <li>
<div class="activity-graph">
    <img src="https://metacpan.org/dist/Parse-RecDescent/activity.svg?res=month" />
    <div class="comment">24 month</div>
</div>
    </li>
    <li class="nav-header">Tools</li>
    <li>
      <a itemprop="downloadUrl" href="https://cpan.metacpan.org/authors/id/J/JT/JTBRAUN/Parse-RecDescent-1.967015.tar.gz">
      Download (<span itemprop="fileSize">172.85KB</span>)</a>
    </li>
    <li>
      <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FJTBRAUN%2FParse-RecDescent-1.967015%2Flib%2FParse%2FRecDescent.pm">
        MetaCPAN Explorer
      </a>
    </li>
    <li>
      <a href="https://metacpan.org/dist/Parse-RecDescent/permissions">
        Permissions
      </a>
    </li>
    <li>
      <a href="https://metacpan.org/dist/Parse-RecDescent/releases.rss">
        Subscribe to distribution
      </a>
    </li>
    <li>
      <button class="btn btn-link" data-toggle="modal" data-target="#metacpan_install-instructions-dialog">
        Install Instructions
      </button>
    </li>
    <li>
      <form action="https://metacpan.org/search">
        <input type="hidden" name="q" value="dist:Parse-RecDescent">
        <input type="search" name="q" placeholder="Search distribution" class="form-control tool-bar-form">
        <input type="submit" style="display: none">
      </form>
    </li>
    <li>
      <form action="https://grep.metacpan.org/search">
        <input type="hidden" name="qd" value="Parse-RecDescent">
        <input type="hidden" name="source" value="metacpan">
        <input type="search" name="q" placeholder="grep distribution" class="form-control tool-bar-form">
        <input type="submit" style="display: none">
     </form>
    </li>
    <li class="version-jump">
<select onchange="document.location.href=&#39;/release/&#39;+this.value+&#39;/view/lib/Parse/RecDescent.pm&#39;" class="form-control tool-bar-form">
  <option disabled selected>Jump to version</option>
<option
  disabled
  value="JTBRAUN/Parse-RecDescent-1.967015"
>1.967015
  (JTBRAUN on 2017-04-04)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967014"
>1.967014
  (JTBRAUN on 2017-04-01)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967013"
>1.967013
  (JTBRAUN on 2015-09-27)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967012"
>1.967012
  (JTBRAUN on 2015-09-13)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_010"
>1.967_010 DEV
  (JTBRAUN on 2013-07-08)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967009"
>1.967009
  (JTBRAUN on 2012-03-16)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_008"
>1.967_008 DEV
  (JTBRAUN on 2012-03-14)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_007"
>1.967_007 DEV
  (JTBRAUN on 2012-02-23)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967006"
>1.967006
  (JTBRAUN on 2012-02-11)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_005"
>1.967_005 DEV
  (JTBRAUN on 2012-02-09)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_004"
>1.967_004 DEV
  (JTBRAUN on 2012-02-08)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967003"
>1.967003
  (JTBRAUN on 2012-01-30)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967002"
>1.967002
  (JTBRAUN on 2012-01-30)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967001"
>1.967001
  (JTBRAUN on 2012-01-29)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.966_002"
>1.966_002 DEV
  (JTBRAUN on 2012-01-23)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.966_000"
>1.966_000 DEV
  (DCONWAY on 2011-06-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.965001"
>1.965001
  (DCONWAY on 2010-04-04)</option>
<optgroup label="BackPAN">'
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967011"
>1.967011
  (JTBRAUN on 2015-09-12)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.964"
>1.964
  (DCONWAY on 2010-02-16)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.963"
>1.963
  (DCONWAY on 2010-01-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.2"
>1.962.2
  (DCONWAY on 2009-08-28)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.1"
>1.962.1
  (DCONWAY on 2009-08-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.0"
>1.962.0
  (DCONWAY on 2009-08-25)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.96.0"
>1.96.0
  (DCONWAY on 2008-10-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-v1.95.1"
>v1.95.1
  (DCONWAY on 2007-09-29)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.94"
>1.94
  (DCONWAY on 2003-04-09)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.93"
>1.93
  (DCONWAY on 2003-04-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.92"
>1.92
  (DCONWAY on 2003-04-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.91"
>1.91
  (DCONWAY on 2003-03-29)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.90"
>1.90
  (DCONWAY on 2003-03-25)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.80"
>1.80
  (DCONWAY on 2001-01-19)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.79"
>1.79
  (DCONWAY on 2000-08-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.78"
>1.78
  (DCONWAY on 2000-03-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.77"
>1.77
  (DCONWAY on 1999-11-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.70"
>1.70
  (DCONWAY on 1999-10-08)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.66"
>1.66
  (DCONWAY on 1999-07-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.65"
>1.65
  (DCONWAY on 1999-05-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.64"
>1.64
  (DCONWAY on 1999-03-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.63"
>1.63
  (DCONWAY on 1999-03-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.61"
>1.61
  (DCONWAY on 1998-10-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.51"
>1.51
  (DCONWAY on 1998-08-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.50"
>1.50
  (DCONWAY on 1998-08-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.43"
>1.43
  (DCONWAY on 1998-08-14)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.42"
>1.42
  (DCONWAY on 1998-08-14)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.41"
>1.41
  (DCONWAY on 1998-08-10)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.35"
>1.35
  (DCONWAY on 1998-06-24)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.33"
>1.33
  (DCONWAY on 1998-05-23)</option>
<option
  
  value="DCONWAY/Parse-RecDescent"
>
  (DCONWAY on 1998-05-21)</option>
</optgroup>
</select>
    </li>
    <li class="version-diff">
<select onchange="document.location.href='/release/JTBRAUN/Parse-RecDescent-1.967015/diff/' + encodeURIComponent(this.value) + '/lib/Parse/RecDescent.pm'
" class="form-control tool-bar-form">
  <option disabled selected>Diff with version</option>
<option
  disabled
  value="JTBRAUN/Parse-RecDescent-1.967015"
>1.967015
  (JTBRAUN on 2017-04-04)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967014"
>1.967014
  (JTBRAUN on 2017-04-01)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967013"
>1.967013
  (JTBRAUN on 2015-09-27)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967012"
>1.967012
  (JTBRAUN on 2015-09-13)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_010"
>1.967_010 DEV
  (JTBRAUN on 2013-07-08)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967009"
>1.967009
  (JTBRAUN on 2012-03-16)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_008"
>1.967_008 DEV
  (JTBRAUN on 2012-03-14)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_007"
>1.967_007 DEV
  (JTBRAUN on 2012-02-23)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967006"
>1.967006
  (JTBRAUN on 2012-02-11)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_005"
>1.967_005 DEV
  (JTBRAUN on 2012-02-09)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_004"
>1.967_004 DEV
  (JTBRAUN on 2012-02-08)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967003"
>1.967003
  (JTBRAUN on 2012-01-30)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967002"
>1.967002
  (JTBRAUN on 2012-01-30)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967001"
>1.967001
  (JTBRAUN on 2012-01-29)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.966_002"
>1.966_002 DEV
  (JTBRAUN on 2012-01-23)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.966_000"
>1.966_000 DEV
  (DCONWAY on 2011-06-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.965001"
>1.965001
  (DCONWAY on 2010-04-04)</option>
<optgroup label="BackPAN">'
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967011"
>1.967011
  (JTBRAUN on 2015-09-12)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.964"
>1.964
  (DCONWAY on 2010-02-16)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.963"
>1.963
  (DCONWAY on 2010-01-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.2"
>1.962.2
  (DCONWAY on 2009-08-28)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.1"
>1.962.1
  (DCONWAY on 2009-08-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.0"
>1.962.0
  (DCONWAY on 2009-08-25)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.96.0"
>1.96.0
  (DCONWAY on 2008-10-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-v1.95.1"
>v1.95.1
  (DCONWAY on 2007-09-29)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.94"
>1.94
  (DCONWAY on 2003-04-09)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.93"
>1.93
  (DCONWAY on 2003-04-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.92"
>1.92
  (DCONWAY on 2003-04-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.91"
>1.91
  (DCONWAY on 2003-03-29)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.90"
>1.90
  (DCONWAY on 2003-03-25)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.80"
>1.80
  (DCONWAY on 2001-01-19)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.79"
>1.79
  (DCONWAY on 2000-08-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.78"
>1.78
  (DCONWAY on 2000-03-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.77"
>1.77
  (DCONWAY on 1999-11-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.70"
>1.70
  (DCONWAY on 1999-10-08)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.66"
>1.66
  (DCONWAY on 1999-07-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.65"
>1.65
  (DCONWAY on 1999-05-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.64"
>1.64
  (DCONWAY on 1999-03-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.63"
>1.63
  (DCONWAY on 1999-03-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.61"
>1.61
  (DCONWAY on 1998-10-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.51"
>1.51
  (DCONWAY on 1998-08-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.50"
>1.50
  (DCONWAY on 1998-08-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.43"
>1.43
  (DCONWAY on 1998-08-14)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.42"
>1.42
  (DCONWAY on 1998-08-14)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.41"
>1.41
  (DCONWAY on 1998-08-10)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.35"
>1.35
  (DCONWAY on 1998-06-24)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.33"
>1.33
  (DCONWAY on 1998-05-23)</option>
<option
  
  value="DCONWAY/Parse-RecDescent"
>
  (DCONWAY on 1998-05-21)</option>
</optgroup>
</select>
    </li>

    <li>
<ul class="dependencies">
  <li class="nav-header">Dependencies</li>
  <li><a href="https://metacpan.org/pod/Test::More" title="Test::More" class="ellipsis">Test::More</a></li>
  <li><a href="https://metacpan.org/pod/Text::Balanced" title="Text::Balanced" class="ellipsis">Text::Balanced</a></li>
  <li><i class="ttip" title="dynamic_config enabled">and possibly others</i></li>
  <li>
    <hr>
  </li>
  <li>
    <a href="https://metacpan.org/module/Parse::RecDescent/requires">Reverse dependencies</a>
  </li>
  <li>
    <a href="http://deps.cpantesters.org/?module=Parse%3A%3ARecDescent">CPAN Testers List</a>
  </li>
  <li>
    <a href="https://cpandeps.grinnz.com/?dist=Parse-RecDescent">Dependency graph</a>
  </li>
</ul>
    </li>
    <li class="nav-header">Permalinks</li>
    <li>
      <a href="https://metacpan.org/release/JTBRAUN/Parse-RecDescent-1.967015/view/lib/Parse/RecDescent.pm">This version</a>
    </li>
    <li>
      <a href="./Parse::RecDescent.html">Latest version</a>
    </li>
    <li>
<div class="plussers">
<div class="nav-header">++ed by:</div>
<div>
<a class="display-all" href="https://metacpan.org/author/DICHI"><img src="https://www.gravatar.com/avatar/943b8bf8f93617a5e3d47ed2c634adae?d=identicon&amp;s=20" title="DICHI" alt="DICHI"></a>
<a class="display-all" href="https://metacpan.org/author/POLETTIX"><img src="https://www.gravatar.com/avatar/391a994c8608830debb68d5a41f28e15?d=identicon&amp;s=20" title="POLETTIX" alt="POLETTIX"></a>
<a class="display-all" href="https://metacpan.org/author/HIDEAKIO"><img src="https://www.gravatar.com/avatar/23b04f6a8c2a559d743cae394b4fc519?d=identicon&amp;s=20" title="HIDEAKIO" alt="HIDEAKIO"></a>
<a class="display-all" href="https://metacpan.org/author/DBOEHMER"><img src="https://www.gravatar.com/avatar/220fbe10dba79664fe84771d5ea32763?d=identicon&amp;s=20" title="DBOEHMER" alt="DBOEHMER"></a>
<a class="display-all" href="https://metacpan.org/author/AZAWAWI"><img src="https://www.gravatar.com/avatar/70ebafdecf974e15bcaba4cbe39efef1?d=identicon&amp;s=20" title="AZAWAWI" alt="AZAWAWI"></a>
</div>
<!-- Display counts of plussers-->
<div>
    <a href="https://metacpan.org/dist/Parse-RecDescent/plussers">6 PAUSE users</a>
</div>
<div>
    13 non-PAUSE users
</div>
</div>
    </li>
    <li>
<div id="metacpan_contributors">
  <div>
    <button class="btn-link"
      onclick="$(this).hide(); $('#metacpan_contributors ul').slideDown(); return false;"
    >and 2 contributors</button>
  </div>
  <ul style="display: none">
    <li class="contributor"
      data-contrib-email="DCONWAY@CPAN.org"
    >
      <img class="gravatar" width="20" height="20" src="https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?d=identicon&amp;s=20" />
      Damian Conway
    </li>
    <li class="contributor"
      data-contrib-email="JTBRAUN@CPAN.org"
    >
      <img class="gravatar" width="20" height="20" src="https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?d=identicon&amp;s=20" />
      Jeremy T. Braun
    </li>
  </ul>
</div>
    </li>
              </ul>
            </div>
          </nav>
          <div class="content-navigation">
<div class="breadcrumbs">
  <span>
    <a data-keyboard-shortcut="g a" rel="author" href="https://metacpan.org/author/JTBRAUN" class="author-name">Jeremy T. Braun</a>
  </span>
  <span>&nbsp;/&nbsp;</span>
  <div class="release dist-release status-latest maturity-released">
    <span class="dropdown"><b class="caret"></b></span>
<select onchange="document.location.href=&#39;/release/&#39;+this.value+&#39;/view/lib/Parse/RecDescent.pm&#39;" class="">
<option
  selected
  value="JTBRAUN/Parse-RecDescent-1.967015"
>1.967015
  (JTBRAUN on 2017-04-04)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967014"
>1.967014
  (JTBRAUN on 2017-04-01)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967013"
>1.967013
  (JTBRAUN on 2015-09-27)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967012"
>1.967012
  (JTBRAUN on 2015-09-13)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_010"
>1.967_010 DEV
  (JTBRAUN on 2013-07-08)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967009"
>1.967009
  (JTBRAUN on 2012-03-16)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_008"
>1.967_008 DEV
  (JTBRAUN on 2012-03-14)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_007"
>1.967_007 DEV
  (JTBRAUN on 2012-02-23)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967006"
>1.967006
  (JTBRAUN on 2012-02-11)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_005"
>1.967_005 DEV
  (JTBRAUN on 2012-02-09)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967_004"
>1.967_004 DEV
  (JTBRAUN on 2012-02-08)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967003"
>1.967003
  (JTBRAUN on 2012-01-30)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967002"
>1.967002
  (JTBRAUN on 2012-01-30)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967001"
>1.967001
  (JTBRAUN on 2012-01-29)</option>
<option
  
  value="JTBRAUN/Parse-RecDescent-1.966_002"
>1.966_002 DEV
  (JTBRAUN on 2012-01-23)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.966_000"
>1.966_000 DEV
  (DCONWAY on 2011-06-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.965001"
>1.965001
  (DCONWAY on 2010-04-04)</option>
<optgroup label="BackPAN">'
<option
  
  value="JTBRAUN/Parse-RecDescent-1.967011"
>1.967011
  (JTBRAUN on 2015-09-12)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.964"
>1.964
  (DCONWAY on 2010-02-16)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.963"
>1.963
  (DCONWAY on 2010-01-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.2"
>1.962.2
  (DCONWAY on 2009-08-28)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.1"
>1.962.1
  (DCONWAY on 2009-08-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.962.0"
>1.962.0
  (DCONWAY on 2009-08-25)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.96.0"
>1.96.0
  (DCONWAY on 2008-10-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-v1.95.1"
>v1.95.1
  (DCONWAY on 2007-09-29)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.94"
>1.94
  (DCONWAY on 2003-04-09)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.93"
>1.93
  (DCONWAY on 2003-04-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.92"
>1.92
  (DCONWAY on 2003-04-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.91"
>1.91
  (DCONWAY on 2003-03-29)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.90"
>1.90
  (DCONWAY on 2003-03-25)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.80"
>1.80
  (DCONWAY on 2001-01-19)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.79"
>1.79
  (DCONWAY on 2000-08-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.78"
>1.78
  (DCONWAY on 2000-03-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.77"
>1.77
  (DCONWAY on 1999-11-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.70"
>1.70
  (DCONWAY on 1999-10-08)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.66"
>1.66
  (DCONWAY on 1999-07-02)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.65"
>1.65
  (DCONWAY on 1999-05-20)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.64"
>1.64
  (DCONWAY on 1999-03-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.63"
>1.63
  (DCONWAY on 1999-03-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.61"
>1.61
  (DCONWAY on 1998-10-21)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.51"
>1.51
  (DCONWAY on 1998-08-27)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.50"
>1.50
  (DCONWAY on 1998-08-26)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.43"
>1.43
  (DCONWAY on 1998-08-14)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.42"
>1.42
  (DCONWAY on 1998-08-14)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.41"
>1.41
  (DCONWAY on 1998-08-10)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.35"
>1.35
  (DCONWAY on 1998-06-24)</option>
<option
  
  value="DCONWAY/Parse-RecDescent-1.33"
>1.33
  (DCONWAY on 1998-05-23)</option>
<option
  
  value="DCONWAY/Parse-RecDescent"
>
  (DCONWAY on 1998-05-21)</option>
</optgroup>
</select>
    <a data-keyboard-shortcut="g d" class="release-name" href="https://metacpan.org/dist/Parse-RecDescent">Parse-RecDescent-1.967015</a>
  </div>
<span class="river-gauge-gauge">
  <svg width="24px"
       height="15px"
       version="1.1"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink">

    <g>
      <title>        River stage three &#10;
          • 129 direct dependents &#10;          • 778 total dependents
      </title>

      <rect x="0"  y="0" width="4" height="15" fill="#7ea3f2" />
      <rect x="5"  y="0" width="4" height="15" fill="#7ea3f2" />
      <rect x="10"  y="0" width="4" height="15" fill="#7ea3f2" />
      <rect x="15"  y="0" width="4" height="15" fill="#e4e2e2" />
      <rect x="20"  y="0" width="4" height="15" fill="#e4e2e2" />
    </g>
  </svg>

</span>
<div id="Parse-RecDescent-fav" class="logged_in">
<form action="https://metacpan.org/account/favorite/add" style="display: inline" onsubmit="return favDistribution(this)">
    <input type="hidden" name="remove" value="0">
    <input type="hidden" name="release" value="Parse-RecDescent-1.967015">
    <input type="hidden" name="author" value="JTBRAUN">
    <input type="hidden" name="distribution" value="Parse-RecDescent">
    <button type="submit" class="favorite highlight"><span>19</span> ++</button>
</form>
</div>
<div class="logged_out">
<a href="./Parse::RecDescent.html" onclick="alert('Please sign in to add favorites'); return false" class="favorite highlight">
<span>19</span> ++</a>
</div>
   / <span>Parse::RecDescent</span>
</div>
          </div>
          <main class="content">


<nav class="toc">
  <div class="toc-header"><strong>Contents</strong></div>
<ul>
  <li><a href="./Parse::RecDescent.html#NAME">NAME</a></li>
  <li><a href="./Parse::RecDescent.html#VERSION">VERSION</a></li>
  <li><a href="./Parse::RecDescent.html#SYNOPSIS">SYNOPSIS</a></li>
  <li><a href="./Parse::RecDescent.html#DESCRIPTION">DESCRIPTION</a>
    <ul>
      <li><a href="./Parse::RecDescent.html#Overview">Overview</a></li>
      <li><a href="./Parse::RecDescent.html#Using-Parse::RecDescent">Using Parse::RecDescent</a></li>
      <li><a href="./Parse::RecDescent.html#Rules">Rules</a></li>
      <li><a href="./Parse::RecDescent.html#Subrules">Subrules</a></li>
      <li><a href="./Parse::RecDescent.html#Tokens">Tokens</a></li>
      <li><a href="./Parse::RecDescent.html#Anonymous-subrules">Anonymous subrules</a></li>
      <li><a href="./Parse::RecDescent.html#Terminal-Separators">Terminal Separators</a></li>
      <li><a href="./Parse::RecDescent.html#Actions">Actions</a></li>
      <li><a href="./Parse::RecDescent.html#Start-up-Actions">Start-up Actions</a></li>
      <li><a href="./Parse::RecDescent.html#Autoactions">Autoactions</a></li>
      <li><a href="./Parse::RecDescent.html#Autotrees">Autotrees</a></li>
      <li><a href="./Parse::RecDescent.html#Autostubbing">Autostubbing</a></li>
      <li><a href="./Parse::RecDescent.html#Look-ahead">Look-ahead</a></li>
      <li><a href="./Parse::RecDescent.html#Directives">Directives</a></li>
      <li><a href="./Parse::RecDescent.html#Subrule-argument-lists">Subrule argument lists</a></li>
      <li><a href="./Parse::RecDescent.html#Alternations">Alternations</a></li>
      <li><a href="./Parse::RecDescent.html#Incremental-Parsing">Incremental Parsing</a></li>
      <li><a href="./Parse::RecDescent.html#Precompiling-parsers">Precompiling parsers</a>
        <ul>
          <li><a href="./Parse::RecDescent.html#Standalone-precompiled-parsers">Standalone precompiled parsers</a></li>
          <li><a href="./Parse::RecDescent.html#Precompiled-runtimes">Precompiled runtimes</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><a href="./Parse::RecDescent.html#GOTCHAS">GOTCHAS</a>
    <ul>
      <li><a href="./Parse::RecDescent.html#1.-Expecting-an-error-to-always-invalidate-a-parse">1. Expecting an error to always invalidate a parse</a></li>
      <li><a href="./Parse::RecDescent.html#2.-Using-a-return-in-an-action">2. Using a return in an action</a></li>
      <li><a href="./Parse::RecDescent.html#2.-Setting-$Parse::RecDescent::skip-at-parse-time">2. Setting $Parse::RecDescent::skip at parse time</a></li>
    </ul>
  </li>
  <li><a href="./Parse::RecDescent.html#DIAGNOSTICS">DIAGNOSTICS</a></li>
  <li><a href="./Parse::RecDescent.html#AUTHOR">AUTHOR</a></li>
  <li><a href="./Parse::RecDescent.html#BUGS-AND-IRRITATIONS">BUGS AND IRRITATIONS</a></li>
  <li><a href="./Parse::RecDescent.html#ON-GOING-ISSUES-AND-FUTURE-DIRECTIONS">ON-GOING ISSUES AND FUTURE DIRECTIONS</a></li>
  <li><a href="./Parse::RecDescent.html#SUPPORT">SUPPORT</a>
    <ul>
      <li><a href="./Parse::RecDescent.html#Source-Code-Repository">Source Code Repository</a></li>
      <li><a href="./Parse::RecDescent.html#Mailing-List">Mailing List</a></li>
      <li><a href="./Parse::RecDescent.html#FAQ">FAQ</a></li>
      <li><a href="./Parse::RecDescent.html#View/Report-Bugs">View/Report Bugs</a></li>
    </ul>
  </li>
  <li><a href="./Parse::RecDescent.html#SEE-ALSO">SEE ALSO</a></li>
  <li><a href="./Parse::RecDescent.html#LICENCE-AND-COPYRIGHT">LICENCE AND COPYRIGHT</a></li>
  <li><a href="./Parse::RecDescent.html#DISCLAIMER-OF-WARRANTY">DISCLAIMER OF WARRANTY</a></li>
</ul></nav>
<div class="pod anchors">
<h1 id="NAME">NAME</h1>

<p>Parse::RecDescent - Generate Recursive-Descent Parsers</p>

<h1 id="VERSION">VERSION</h1>

<p>This document describes version 1.967015 of Parse::RecDescent released April 4th, 2017.</p>

<h1 id="SYNOPSIS">SYNOPSIS</h1>

<pre><code> use Parse::RecDescent;

 # Generate a parser from the specification in $grammar:

     $parser = new Parse::RecDescent ($grammar);

 # Generate a parser from the specification in $othergrammar

     $anotherparser = new Parse::RecDescent ($othergrammar);


 # Parse $text using rule &#39;startrule&#39; (which must be
 # defined in $grammar):

    $parser-&gt;startrule($text);


 # Parse $text using rule &#39;otherrule&#39; (which must also
 # be defined in $grammar):

     $parser-&gt;otherrule($text);


 # Change the universal token prefix pattern
 # before building a grammar
 # (the default is: &#39;\s*&#39;):

    $Parse::RecDescent::skip = &#39;[ \t]+&#39;;


 # Replace productions of existing rules (or create new ones)
 # with the productions defined in $newgrammar:

    $parser-&gt;Replace($newgrammar);


 # Extend existing rules (or create new ones)
 # by adding extra productions defined in $moregrammar:

    $parser-&gt;Extend($moregrammar);


 # Global flags (useful as command line arguments under -s):

    $::RD_ERRORS       # unless undefined, report fatal errors
    $::RD_WARN         # unless undefined, also report non-fatal problems
    $::RD_HINT         # if defined, also suggestion remedies
    $::RD_TRACE        # if defined, also trace parsers&#39; behaviour
    $::RD_AUTOSTUB     # if defined, generates &quot;stubs&quot; for undefined rules
    $::RD_AUTOACTION   # if defined, appends specified action to productions</code></pre>

<h1 id="DESCRIPTION">DESCRIPTION</h1>

<h2 id="Overview">Overview</h2>

<p>Parse::RecDescent incrementally generates top-down recursive-descent text parsers from simple <i>yacc</i>-like grammar specifications. It provides:</p>

<ul>

<li><p>Regular expressions or literal strings as terminals (tokens),</p>

</li>
<li><p>Multiple (non-contiguous) productions for any rule,</p>

</li>
<li><p>Repeated and optional subrules within productions,</p>

</li>
<li><p>Full access to Perl within actions specified as part of the grammar,</p>

</li>
<li><p>Simple automated error reporting during parser generation and parsing,</p>

</li>
<li><p>The ability to commit to, uncommit to, or reject particular productions during a parse,</p>

</li>
<li><p>The ability to pass data up and down the parse tree (&quot;down&quot; via subrule argument lists, &quot;up&quot; via subrule return values)</p>

</li>
<li><p>Incremental extension of the parsing grammar (even during a parse),</p>

</li>
<li><p>Precompilation of parser objects,</p>

</li>
<li><p>User-definable reduce-reduce conflict resolution via &quot;scoring&quot; of matching productions.</p>

</li>
</ul>

<h2 id="Using-Parse::RecDescent"><a id="Using"></a>Using <code>Parse::RecDescent</code></h2>

<p>Parser objects are created by calling <code>Parse::RecDescent::new</code>, passing in a grammar specification (see the following subsections). If the grammar is correct, <code>new</code> returns a blessed reference which can then be used to initiate parsing through any rule specified in the original grammar. A typical sequence looks like this:</p>

<pre><code>    $grammar = q {
        # GRAMMAR SPECIFICATION HERE
         };

    $parser = new Parse::RecDescent ($grammar) or die &quot;Bad grammar!\n&quot;;

    # acquire $text

    defined $parser-&gt;startrule($text) or print &quot;Bad text!\n&quot;;</code></pre>

<p>The rule through which parsing is initiated must be explicitly defined in the grammar (i.e. for the above example, the grammar must include a rule of the form: &quot;startrule: &lt;subrules&gt;&quot;.</p>

<p>If the starting rule succeeds, its value (see below) is returned. Failure to generate the original parser or failure to match a text is indicated by returning <code>undef</code>. Note that it&#39;s easy to set up grammars that can succeed, but which return a value of 0, &quot;0&quot;, or &quot;&quot;. So don&#39;t be tempted to write:</p>

<pre><code>    $parser-&gt;startrule($text) or print &quot;Bad text!\n&quot;;</code></pre>

<p>Normally, the parser has no effect on the original text. So in the previous example the value of $text would be unchanged after having been parsed.</p>

<p>If, however, the text to be matched is passed by reference:</p>

<pre><code>    $parser-&gt;startrule(\$text)</code></pre>

<p>then any text which was consumed during the match will be removed from the start of $text.</p>

<h2 id="Rules">Rules</h2>

<p>In the grammar from which the parser is built, rules are specified by giving an identifier (which must satisfy /[A-Za-z]\w*/), followed by a colon <i>on the same line</i>, followed by one or more productions, separated by single vertical bars. The layout of the productions is entirely free-format:</p>

<pre><code>    rule1:  production1
     |  production2 |
    production3 | production4</code></pre>

<p>At any point in the grammar previously defined rules may be extended with additional productions. This is achieved by redeclaring the rule with the new productions. Thus:</p>

<pre><code>    rule1: a | b | c
    rule2: d | e | f
    rule1: g | h</code></pre>

<p>is exactly equivalent to:</p>

<pre><code>    rule1: a | b | c | g | h
    rule2: d | e | f</code></pre>

<p>Each production in a rule consists of zero or more items, each of which may be either: the name of another rule to be matched (a &quot;subrule&quot;), a pattern or string literal to be matched directly (a &quot;token&quot;), a block of Perl code to be executed (an &quot;action&quot;), a special instruction to the parser (a &quot;directive&quot;), or a standard Perl comment (which is ignored).</p>

<p>A rule matches a text if one of its productions matches. A production matches if each of its items match consecutive substrings of the text. The productions of a rule being matched are tried in the same order that they appear in the original grammar, and the first matching production terminates the match attempt (successfully). If all productions are tried and none matches, the match attempt fails.</p>

<p>Note that this behaviour is quite different from the &quot;prefer the longer match&quot; behaviour of <i>yacc</i>. For example, if <i>yacc</i> were parsing the rule:</p>

<pre><code>    seq : &#39;A&#39; &#39;B&#39;
    | &#39;A&#39; &#39;B&#39; &#39;C&#39;</code></pre>

<p>upon matching &quot;AB&quot; it would look ahead to see if a &#39;C&#39; is next and, if so, will match the second production in preference to the first. In other words, <i>yacc</i> effectively tries all the productions of a rule breadth-first in parallel, and selects the &quot;best&quot; match, where &quot;best&quot; means longest (note that this is a gross simplification of the true behaviour of <i>yacc</i> but it will do for our purposes).</p>

<p>In contrast, <code>Parse::RecDescent</code> tries each production depth-first in sequence, and selects the &quot;best&quot; match, where &quot;best&quot; means first. This is the fundamental difference between &quot;bottom-up&quot; and &quot;recursive descent&quot; parsing.</p>

<p>Each successfully matched item in a production is assigned a value, which can be accessed in subsequent actions within the same production (or, in some cases, as the return value of a successful subrule call). Unsuccessful items don&#39;t have an associated value, since the failure of an item causes the entire surrounding production to immediately fail. The following sections describe the various types of items and their success values.</p>

<h2 id="Subrules">Subrules</h2>

<p>A subrule which appears in a production is an instruction to the parser to attempt to match the named rule at that point in the text being parsed. If the named subrule is not defined when requested the production containing it immediately fails (unless it was &quot;autostubbed&quot; - see <a href="https://metacpan.org/pod/Autostubbing">Autostubbing</a>).</p>

<p>A rule may (recursively) call itself as a subrule, but <i>not</i> as the left-most item in any of its productions (since such recursions are usually non-terminating).</p>

<p>The value associated with a subrule is the value associated with its <code>$return</code> variable (see <a href="./Parse::RecDescent.html#Actions">&quot;Actions&quot;</a> below), or with the last successfully matched item in the subrule match.</p>

<p>Subrules may also be specified with a trailing repetition specifier, indicating that they are to be (greedily) matched the specified number of times. The available specifiers are:</p>

<pre><code>    subrule(?)  # Match one-or-zero times
    subrule(s)  # Match one-or-more times
    subrule(s?) # Match zero-or-more times
    subrule(N)  # Match exactly N times for integer N &gt; 0
    subrule(N..M)   # Match between N and M times
    subrule(..M)    # Match between 1 and M times
    subrule(N..)    # Match at least N times</code></pre>

<p>Repeated subrules keep matching until either the subrule fails to match, or it has matched the minimal number of times but fails to consume any of the parsed text (this second condition prevents the subrule matching forever in some cases).</p>

<p>Since a repeated subrule may match many instances of the subrule itself, the value associated with it is not a simple scalar, but rather a reference to a list of scalars, each of which is the value associated with one of the individual subrule matches. In other words in the rule:</p>

<pre><code>    program: statement(s)</code></pre>

<p>the value associated with the repeated subrule &quot;statement(s)&quot; is a reference to an array containing the values matched by each call to the individual subrule &quot;statement&quot;.</p>

<p>Repetition modifiers may include a separator pattern:</p>

<pre><code>    program: statement(s /;/)</code></pre>

<p>specifying some sequence of characters to be skipped between each repetition. This is really just a shorthand for the &lt;leftop:...&gt; directive (see below).</p>

<h2 id="Tokens">Tokens</h2>

<p>If a quote-delimited string or a Perl regex appears in a production, the parser attempts to match that string or pattern at that point in the text. For example:</p>

<pre><code>    typedef: &quot;typedef&quot; typename identifier &#39;;&#39;

    identifier: /[A-Za-z_][A-Za-z0-9_]*/</code></pre>

<p>As in regular Perl, a single quoted string is uninterpolated, whilst a double-quoted string or a pattern is interpolated (at the time of matching, <i>not</i> when the parser is constructed). Hence, it is possible to define rules in which tokens can be set at run-time:</p>

<pre><code>    typedef: &quot;$::typedefkeyword&quot; typename identifier &#39;;&#39;

    identifier: /$::identpat/</code></pre>

<p>Note that, since each rule is implemented inside a special namespace belonging to its parser, it is necessary to explicitly quantify variables from the main package.</p>

<p>Regex tokens can be specified using just slashes as delimiters or with the explicit <code>m&lt;delimiter&gt;......&lt;delimiter&gt;</code> syntax:</p>

<pre><code>    typedef: &quot;typedef&quot; typename identifier &#39;;&#39;

    typename: /[A-Za-z_][A-Za-z0-9_]*/

    identifier: m{[A-Za-z_][A-Za-z0-9_]*}</code></pre>

<p>A regex of either type can also have any valid trailing parameter(s) (that is, any of [cgimsox]):</p>

<pre><code>    typedef: &quot;typedef&quot; typename identifier &#39;;&#39;

    identifier: / [a-z_]        # LEADING ALPHA OR UNDERSCORE
          [a-z0-9_]*    # THEN DIGITS ALSO ALLOWED
        /ix     # CASE/SPACE/COMMENT INSENSITIVE</code></pre>

<p>The value associated with any successfully matched token is a string containing the actual text which was matched by the token.</p>

<p>It is important to remember that, since each grammar is specified in a Perl string, all instances of the universal escape character &#39;\&#39; within a grammar must be &quot;doubled&quot;, so that they interpolate to single &#39;\&#39;s when the string is compiled. For example, to use the grammar:</p>

<pre><code>    word:       /\S+/ | backslash
    line:       prefix word(s) &quot;\n&quot;
    backslash:  &#39;\\&#39;</code></pre>

<p>the following code is required:</p>

<pre><code>    $parser = new Parse::RecDescent (q{

        word:   /\\S+/ | backslash
        line:   prefix word(s) &quot;\\n&quot;
        backslash:  &#39;\\\\&#39;

    });</code></pre>

<h2 id="Anonymous-subrules"><a id="Anonymous"></a>Anonymous subrules</h2>

<p>Parentheses introduce a nested scope that is very like a call to an anonymous subrule. Hence they are useful for &quot;in-lining&quot; subroutine calls, and other kinds of grouping behaviour. For example, instead of:</p>

<pre><code>    word:       /\S+/ | backslash
    line:       prefix word(s) &quot;\n&quot;</code></pre>

<p>you could write:</p>

<pre><code>    line:       prefix ( /\S+/ | backslash )(s) &quot;\n&quot;</code></pre>

<p>and get exactly the same effects.</p>

<p>Parentheses are also use for collecting unrepeated alternations within a single production.</p>

<pre><code>    secret_identity: &quot;Mr&quot; (&quot;Incredible&quot;|&quot;Fantastic&quot;|&quot;Sheen&quot;) &quot;, Esq.&quot;</code></pre>

<h2 id="Terminal-Separators"><a id="Terminal"></a>Terminal Separators</h2>

<p>For the purpose of matching, each terminal in a production is considered to be preceded by a &quot;prefix&quot; - a pattern which must be matched before a token match is attempted. By default, the prefix is optional whitespace (which always matches, at least trivially), but this default may be reset in any production.</p>

<p>The variable <code>$Parse::RecDescent::skip</code> stores the universal prefix, which is the default for all terminal matches in all parsers built with <code>Parse::RecDescent</code>.</p>

<p>If you want to change the universal prefix using <code>$Parse::RecDescent::skip</code>, be careful to set it <i>before</i> creating the grammar object, because it is applied statically (when a grammar is built) rather than dynamically (when the grammar is used). Alternatively you can provide a global <code>&lt;skip:...&gt;</code> directive in your grammar before any rules (described later).</p>

<p>The prefix for an individual production can be altered by using the <code>&lt;skip:...&gt;</code> directive (described later). Setting this directive in the top-level rule is an alternative approach to setting <code>$Parse::RecDescent::skip</code> before creating the object, but in this case you don&#39;t get the intended skipping behaviour if you directly invoke methods different from the top-level rule.</p>

<h2 id="Actions">Actions</h2>

<p>An action is a block of Perl code which is to be executed (as the block of a <code>do</code> statement) when the parser reaches that point in a production. The action executes within a special namespace belonging to the active parser, so care must be taken in correctly qualifying variable names (see also <a href="./Parse::RecDescent.html#Start-up-Actions">&quot;Start-up Actions&quot;</a> below).</p>

<p>The action is considered to succeed if the final value of the block is defined (that is, if the implied <code>do</code> statement evaluates to a defined value - <i>even one which would be treated as &quot;false&quot;</i>). Note that the value associated with a successful action is also the final value in the block.</p>

<p>An action will <i>fail</i> if its last evaluated value is <code>undef</code>. This is surprisingly easy to accomplish by accident. For instance, here&#39;s an infuriating case of an action that makes its production fail, but only when debugging <i>isn&#39;t</i> activated:</p>

<pre><code>    description: name rank serial_number
        { print &quot;Got $item[2] $item[1] ($item[3])\n&quot;
        if $::debugging
        }</code></pre>

<p>If <code>$debugging</code> is false, no statement in the block is executed, so the final value is <code>undef</code>, and the entire production fails. The solution is:</p>

<pre><code>    description: name rank serial_number
        { print &quot;Got $item[2] $item[1] ($item[3])\n&quot;
        if $::debugging;
          1;
        }</code></pre>

<p>Within an action, a number of useful parse-time variables are available in the special parser namespace (there are other variables also accessible, but meddling with them will probably just break your parser. As a general rule, if you avoid referring to unqualified variables - especially those starting with an underscore - inside an action, things should be okay):</p>

<dl>

<dt id="@item-and-%item"><a id="item-and--item"></a><code>@item</code> and <code>%item</code></dt>
<dd>

<p>The array slice <code>@item[1..$#item]</code> stores the value associated with each item (that is, each subrule, token, or action) in the current production. The analogy is to <code>$1</code>, <code>$2</code>, etc. in a <i>yacc</i> grammar. Note that, for obvious reasons, <code>@item</code> only contains the values of items <i>before</i> the current point in the production.</p>

<p>The first element (<code>$item[0]</code>) stores the name of the current rule being matched.</p>

<p><code>@item</code> is a standard Perl array, so it can also be indexed with negative numbers, representing the number of items <i>back</i> from the current position in the parse:</p>

<pre><code>    stuff: /various/ bits &#39;and&#39; pieces &quot;then&quot; data &#39;end&#39;
        { print $item[-2] }  # PRINTS data
             # (EASIER THAN: $item[6])</code></pre>

<p>The <code>%item</code> hash complements the &lt;@item&gt; array, providing named access to the same item values:</p>

<pre><code>    stuff: /various/ bits &#39;and&#39; pieces &quot;then&quot; data &#39;end&#39;
        { print $item{data}  # PRINTS data
             # (EVEN EASIER THAN USING @item)</code></pre>

<p>The results of named subrules are stored in the hash under each subrule&#39;s name (including the repetition specifier, if any), whilst all other items are stored under a &quot;named positional&quot; key that indicates their ordinal position within their item type: __STRING<i>n</i>__, __PATTERN<i>n</i>__, __DIRECTIVE<i>n</i>__, __ACTION<i>n</i>__:</p>

<pre><code>    stuff: /various/ bits &#39;and&#39; pieces &quot;then&quot; data &#39;end&#39; { save }
        { print $item{__PATTERN1__}, # PRINTS &#39;various&#39;
        $item{__STRING2__},  # PRINTS &#39;then&#39;
        $item{__ACTION1__},  # PRINTS RETURN
                 # VALUE OF save
        }</code></pre>

<p>If you want proper <i>named</i> access to patterns or literals, you need to turn them into separate rules:</p>

<pre><code>    stuff: various bits &#39;and&#39; pieces &quot;then&quot; data &#39;end&#39;
        { print $item{various}  # PRINTS various
        }

    various: /various/</code></pre>

<p>The special entry <code>$item{__RULE__}</code> stores the name of the current rule (i.e. the same value as <code>$item[0]</code>.</p>

<p>The advantage of using <code>%item</code>, instead of <code>@items</code> is that it removes the need to track items positions that may change as a grammar evolves. For example, adding an interim <code>&lt;skip&gt;</code> directive of action can silently ruin a trailing action, by moving an <code>@item</code> element &quot;down&quot; the array one place. In contrast, the named entry of <code>%item</code> is unaffected by such an insertion.</p>

<p>A limitation of the <code>%item</code> hash is that it only records the <i>last</i> value of a particular subrule. For example:</p>

<pre><code>    range: &#39;(&#39; number &#39;..&#39; number )&#39;
        { $return = $item{number} }</code></pre>

<p>will return only the value corresponding to the <i>second</i> match of the <code>number</code> subrule. In other words, successive calls to a subrule overwrite the corresponding entry in <code>%item</code>. Once again, the solution is to rename each subrule in its own rule:</p>

<pre><code>    range: &#39;(&#39; from_num &#39;..&#39; to_num &#39;)&#39;
        { $return = $item{from_num} }

    from_num: number
    to_num:   number</code></pre>

</dd>
<dt id="@arg-and-%arg"><a id="arg-and--arg"></a><code>@arg</code> and <code>%arg</code></dt>
<dd>

<p>The array <code>@arg</code> and the hash <code>%arg</code> store any arguments passed to the rule from some other rule (see <a href="./Parse::RecDescent.html#Subrule-argument-lists">&quot;Subrule argument lists&quot;</a>). Changes to the elements of either variable do not propagate back to the calling rule (data can be passed back from a subrule via the <code>$return</code> variable - see next item).</p>

</dd>
<dt id="$return"><a id="return"></a><code>$return</code></dt>
<dd>

<p>If a value is assigned to <code>$return</code> within an action, that value is returned if the production containing the action eventually matches successfully. Note that setting <code>$return</code> <i>doesn&#39;t</i> cause the current production to succeed. It merely tells it what to return if it <i>does</i> succeed. Hence <code>$return</code> is analogous to <code>$$</code> in a <i>yacc</i> grammar.</p>

<p>If <code>$return</code> is not assigned within a production, the value of the last component of the production (namely: <code>$item[$#item]</code>) is returned if the production succeeds.</p>

</dd>
<dt id="$commit"><a id="commit"></a><code>$commit</code></dt>
<dd>

<p>The current state of commitment to the current production (see <a href="./Parse::RecDescent.html#Directives">&quot;Directives&quot;</a> below).</p>

</dd>
<dt id="$skip"><a id="skip"></a><code>$skip</code></dt>
<dd>

<p>The current terminal prefix (see <a href="./Parse::RecDescent.html#Directives">&quot;Directives&quot;</a> below).</p>

</dd>
<dt id="$text"><a id="text"></a><code>$text</code></dt>
<dd>

<p>The remaining (unparsed) text. Changes to <code>$text</code> <i>do not propagate</i> out of unsuccessful productions, but <i>do</i> survive successful productions. Hence it is possible to dynamically alter the text being parsed - for example, to provide a <code>#include</code>-like facility:</p>

<pre><code>    hash_include: &#39;#include&#39; filename
        { $text = ::loadfile($item[2]) . $text }

    filename: &#39;&lt;&#39; /[a-z0-9._-]+/i &#39;&gt;&#39;  { $return = $item[2] }
    | &#39;&quot;&#39; /[a-z0-9._-]+/i &#39;&quot;&#39;  { $return = $item[2] }</code></pre>

</dd>
<dt id="$thisline-and-$prevline"><a id="thisline-and--prevline"></a><code>$thisline</code> and <code>$prevline</code></dt>
<dd>

<p><code>$thisline</code> stores the current line number within the current parse (starting from 1). <code>$prevline</code> stores the line number for the last character which was already successfully parsed (this will be different from <code>$thisline</code> at the end of each line).</p>

<p>For efficiency, <code>$thisline</code> and <code>$prevline</code> are actually tied hashes, and only recompute the required line number when the variable&#39;s value is used.</p>

<p>Assignment to <code>$thisline</code> adjusts the line number calculator, so that it believes that the current line number is the value being assigned. Note that this adjustment will be reflected in all subsequent line numbers calculations.</p>

<p>Modifying the value of the variable <code>$text</code> (as in the previous <code>hash_include</code> example, for instance) will confuse the line counting mechanism. To prevent this, you should call <code>Parse::RecDescent::LineCounter::resync($thisline)</code> <i>immediately</i> after any assignment to the variable <code>$text</code> (or, at least, before the next attempt to use <code>$thisline</code>).</p>

<p>Note that if a production fails after assigning to or resync&#39;ing <code>$thisline</code>, the parser&#39;s line counter mechanism will usually be corrupted.</p>

<p>Also see the entry for <code>@itempos</code>.</p>

<p>The line number can be set to values other than 1, by calling the start rule with a second argument. For example:</p>

<pre><code>    $parser = new Parse::RecDescent ($grammar);

    $parser-&gt;input($text, 10);  # START LINE NUMBERS AT 10</code></pre>

</dd>
<dt id="$thiscolumn-and-$prevcolumn"><a id="thiscolumn-and--prevcolumn"></a><code>$thiscolumn</code> and <code>$prevcolumn</code></dt>
<dd>

<p><code>$thiscolumn</code> stores the current column number within the current line being parsed (starting from 1). <code>$prevcolumn</code> stores the column number of the last character which was actually successfully parsed. Usually <code>$prevcolumn == $thiscolumn-1</code>, but not at the end of lines.</p>

<p>For efficiency, <code>$thiscolumn</code> and <code>$prevcolumn</code> are actually tied hashes, and only recompute the required column number when the variable&#39;s value is used.</p>

<p>Assignment to <code>$thiscolumn</code> or <code>$prevcolumn</code> is a fatal error.</p>

<p>Modifying the value of the variable <code>$text</code> (as in the previous <code>hash_include</code> example, for instance) may confuse the column counting mechanism.</p>

<p>Note that <code>$thiscolumn</code> reports the column number <i>before</i> any whitespace that might be skipped before reading a token. Hence if you wish to know where a token started (and ended) use something like this:</p>

<pre><code>    rule: token1 token2 startcol token3 endcol token4
        { print &quot;token3: columns $item[3] to $item[5]&quot;; }

    startcol: &#39;&#39; { $thiscolumn }    # NEED THE &#39;&#39; TO STEP PAST TOKEN SEP
    endcol:  { $prevcolumn }</code></pre>

<p>Also see the entry for <code>@itempos</code>.</p>

</dd>
<dt id="$thisoffset-and-$prevoffset"><a id="thisoffset-and--prevoffset"></a><code>$thisoffset</code> and <code>$prevoffset</code></dt>
<dd>

<p><code>$thisoffset</code> stores the offset of the current parsing position within the complete text being parsed (starting from 0). <code>$prevoffset</code> stores the offset of the last character which was actually successfully parsed. In all cases <code>$prevoffset == $thisoffset-1</code>.</p>

<p>For efficiency, <code>$thisoffset</code> and <code>$prevoffset</code> are actually tied hashes, and only recompute the required offset when the variable&#39;s value is used.</p>

<p>Assignment to <code>$thisoffset</code> or &lt;$prevoffset&gt; is a fatal error.</p>

<p>Modifying the value of the variable <code>$text</code> will <i>not</i> affect the offset counting mechanism.</p>

<p>Also see the entry for <code>@itempos</code>.</p>

</dd>
<dt id="@itempos"><a id="itempos"></a><code>@itempos</code></dt>
<dd>

<p>The array <code>@itempos</code> stores a hash reference corresponding to each element of <code>@item</code>. The elements of the hash provide the following:</p>

<pre><code>    $itempos[$n]{offset}{from}  # VALUE OF $thisoffset BEFORE $item[$n]
    $itempos[$n]{offset}{to}    # VALUE OF $prevoffset AFTER $item[$n]
    $itempos[$n]{line}{from}    # VALUE OF $thisline BEFORE $item[$n]
    $itempos[$n]{line}{to}  # VALUE OF $prevline AFTER $item[$n]
    $itempos[$n]{column}{from}  # VALUE OF $thiscolumn BEFORE $item[$n]
    $itempos[$n]{column}{to}    # VALUE OF $prevcolumn AFTER $item[$n]</code></pre>

<p>Note that the various <code>$itempos[$n]...{from}</code> values record the appropriate value <i>after</i> any token prefix has been skipped.</p>

<p>Hence, instead of the somewhat tedious and error-prone:</p>

<pre><code>    rule: startcol token1 endcol
      startcol token2 endcol
      startcol token3 endcol
        { print &quot;token1: columns $item[1]
              to $item[3]
         token2: columns $item[4]
              to $item[6]
         token3: columns $item[7]
              to $item[9]&quot; }

    startcol: &#39;&#39; { $thiscolumn }    # NEED THE &#39;&#39; TO STEP PAST TOKEN SEP
    endcol:  { $prevcolumn }</code></pre>

<p>it is possible to write:</p>

<pre><code>    rule: token1 token2 token3
        { print &quot;token1: columns $itempos[1]{column}{from}
              to $itempos[1]{column}{to}
         token2: columns $itempos[2]{column}{from}
              to $itempos[2]{column}{to}
         token3: columns $itempos[3]{column}{from}
              to $itempos[3]{column}{to}&quot; }</code></pre>

<p>Note however that (in the current implementation) the use of <code>@itempos</code> anywhere in a grammar implies that item positioning information is collected <i>everywhere</i> during the parse. Depending on the grammar and the size of the text to be parsed, this may be prohibitively expensive and the explicit use of <code>$thisline</code>, <code>$thiscolumn</code>, etc. may be a better choice.</p>

</dd>
<dt id="$thisparser"><a id="thisparser"></a><code>$thisparser</code></dt>
<dd>

<p>A reference to the <span style="white-space: nowrap;"><code>Parse::RecDescent</code></span> object through which parsing was initiated.</p>

<p>The value of <code>$thisparser</code> propagates down the subrules of a parse but not back up. Hence, you can invoke subrules from another parser for the scope of the current rule as follows:</p>

<pre><code>    rule: subrule1 subrule2
    | { $thisparser = $::otherparser } &lt;reject&gt;
    | subrule3 subrule4
    | subrule5</code></pre>

<p>The result is that the production calls &quot;subrule1&quot; and &quot;subrule2&quot; of the current parser, and the remaining productions call the named subrules from <code>$::otherparser</code>. Note, however that &quot;Bad Things&quot; will happen if <code>::otherparser</code> isn&#39;t a blessed reference and/or doesn&#39;t have methods with the same names as the required subrules!</p>

</dd>
<dt id="$thisrule"><a id="thisrule"></a><code>$thisrule</code></dt>
<dd>

<p>A reference to the <span style="white-space: nowrap;"><code>Parse::RecDescent::Rule</code></span> object corresponding to the rule currently being matched.</p>

</dd>
<dt id="$thisprod"><a id="thisprod"></a><code>$thisprod</code></dt>
<dd>

<p>A reference to the <span style="white-space: nowrap;"><code>Parse::RecDescent::Production</code></span> object corresponding to the production currently being matched.</p>

</dd>
<dt id="$score-and-$score_return"><a id="score-and--score_return"></a><code>$score</code> and <code>$score_return</code></dt>
<dd>

<p>$score stores the best production score to date, as specified by an earlier <code>&lt;score:...&gt;</code> directive. $score_return stores the corresponding return value for the successful production.</p>

<p>See <a href="./Parse::RecDescent.html#Scored-productions">&quot;Scored productions&quot;</a>.</p>

</dd>
</dl>

<p><b>Warning:</b> the parser relies on the information in the various <code>this...</code> objects in some non-obvious ways. Tinkering with the other members of these objects will probably cause Bad Things to happen, unless you <i>really</i> know what you&#39;re doing. The only exception to this advice is that the use of <code>$this...-&gt;{local}</code> is always safe.</p>

<h2 id="Start-up-Actions"><a id="Start"></a>Start-up Actions</h2>

<p>Any actions which appear <i>before</i> the first rule definition in a grammar are treated as &quot;start-up&quot; actions. Each such action is stripped of its outermost brackets and then evaluated (in the parser&#39;s special namespace) just before the rules of the grammar are first compiled.</p>

<p>The main use of start-up actions is to declare local variables within the parser&#39;s special namespace:</p>

<pre><code>    { my $lastitem = &#39;???&#39;; }

    list: item(s)   { $return = $lastitem }

    item: book  { $lastitem = &#39;book&#39;; }
      bell  { $lastitem = &#39;bell&#39;; }
      candle    { $lastitem = &#39;candle&#39;; }</code></pre>

<p>but start-up actions can be used to execute <i>any</i> valid Perl code within a parser&#39;s special namespace.</p>

<p>Start-up actions can appear within a grammar extension or replacement (that is, a partial grammar installed via <code>Parse::RecDescent::Extend()</code> or <code>Parse::RecDescent::Replace()</code> - see <a href="./Parse::RecDescent.html#Incremental-Parsing">&quot;Incremental Parsing&quot;</a>), and will be executed before the new grammar is installed. Note, however, that a particular start-up action is only ever executed once.</p>

<h2 id="Autoactions">Autoactions</h2>

<p>It is sometimes desirable to be able to specify a default action to be taken at the end of every production (for example, in order to easily build a parse tree). If the variable <code>$::RD_AUTOACTION</code> is defined when <code>Parse::RecDescent::new()</code> is called, the contents of that variable are treated as a specification of an action which is to appended to each production in the corresponding grammar.</p>

<p>Alternatively, you can hard-code the autoaction within a grammar, using the <code>&lt;autoaction:...&gt;</code> directive.</p>

<p>So, for example, to construct a simple parse tree you could write:</p>

<pre><code>    $::RD_AUTOACTION = q { [@item] };

    parser = Parse::RecDescent-&gt;new(q{
    expression: and_expr &#39;||&#39; expression | and_expr
    and_expr:   not_expr &#39;&amp;&amp;&#39; and_expr   | not_expr
    not_expr:   &#39;!&#39; brack_expr       | brack_expr
    brack_expr: &#39;(&#39; expression &#39;)&#39;       | identifier
    identifier: /[a-z]+/i
    });</code></pre>

<p>or:</p>

<pre><code>    parser = Parse::RecDescent-&gt;new(q{
    &lt;autoaction: { [@item] } &gt;

    expression: and_expr &#39;||&#39; expression | and_expr
    and_expr:   not_expr &#39;&amp;&amp;&#39; and_expr   | not_expr
    not_expr:   &#39;!&#39; brack_expr       | brack_expr
    brack_expr: &#39;(&#39; expression &#39;)&#39;       | identifier
    identifier: /[a-z]+/i
    });</code></pre>

<p>Either of these is equivalent to:</p>

<pre><code>    parser = new Parse::RecDescent (q{
    expression: and_expr &#39;||&#39; expression
        { [@item] }
      | and_expr
        { [@item] }

    and_expr:   not_expr &#39;&amp;&amp;&#39; and_expr
        { [@item] }
    |   not_expr
        { [@item] }

    not_expr:   &#39;!&#39; brack_expr
        { [@item] }
    |   brack_expr
        { [@item] }

    brack_expr: &#39;(&#39; expression &#39;)&#39;
        { [@item] }
      | identifier
        { [@item] }

    identifier: /[a-z]+/i
        { [@item] }
    });</code></pre>

<p>Alternatively, we could take an object-oriented approach, use different classes for each node (and also eliminating redundant intermediate nodes):</p>

<pre><code>    $::RD_AUTOACTION = q
      { $#item==1 ? $item[1] : &quot;$item[0]_node&quot;-&gt;new(@item[1..$#item]) };

    parser = Parse::RecDescent-&gt;new(q{
        expression: and_expr &#39;||&#39; expression | and_expr
        and_expr:   not_expr &#39;&amp;&amp;&#39; and_expr   | not_expr
        not_expr:   &#39;!&#39; brack_expr           | brack_expr
        brack_expr: &#39;(&#39; expression &#39;)&#39;       | identifier
        identifier: /[a-z]+/i
    });</code></pre>

<p>or:</p>

<pre><code>    parser = Parse::RecDescent-&gt;new(q{
        &lt;autoaction:
          $#item==1 ? $item[1] : &quot;$item[0]_node&quot;-&gt;new(@item[1..$#item])
        &gt;

        expression: and_expr &#39;||&#39; expression | and_expr
        and_expr:   not_expr &#39;&amp;&amp;&#39; and_expr   | not_expr
        not_expr:   &#39;!&#39; brack_expr           | brack_expr
        brack_expr: &#39;(&#39; expression &#39;)&#39;       | identifier
        identifier: /[a-z]+/i
    });</code></pre>

<p>which are equivalent to:</p>

<pre><code>    parser = Parse::RecDescent-&gt;new(q{
        expression: and_expr &#39;||&#39; expression
            { &quot;expression_node&quot;-&gt;new(@item[1..3]) }
        | and_expr

        and_expr:   not_expr &#39;&amp;&amp;&#39; and_expr
            { &quot;and_expr_node&quot;-&gt;new(@item[1..3]) }
        |   not_expr

        not_expr:   &#39;!&#39; brack_expr
            { &quot;not_expr_node&quot;-&gt;new(@item[1..2]) }
        |   brack_expr

        brack_expr: &#39;(&#39; expression &#39;)&#39;
            { &quot;brack_expr_node&quot;-&gt;new(@item[1..3]) }
        | identifier

        identifier: /[a-z]+/i
            { &quot;identifer_node&quot;-&gt;new(@item[1]) }
    });</code></pre>

<p>Note that, if a production already ends in an action, no autoaction is appended to it. For example, in this version:</p>

<pre><code>    $::RD_AUTOACTION = q
      { $#item==1 ? $item[1] : &quot;$item[0]_node&quot;-&gt;new(@item[1..$#item]) };

    parser = Parse::RecDescent-&gt;new(q{
        expression: and_expr &#39;&amp;&amp;&#39; expression | and_expr
        and_expr:   not_expr &#39;&amp;&amp;&#39; and_expr   | not_expr
        not_expr:   &#39;!&#39; brack_expr           | brack_expr
        brack_expr: &#39;(&#39; expression &#39;)&#39;       | identifier
        identifier: /[a-z]+/i
            { &#39;terminal_node&#39;-&gt;new($item[1]) }
    });</code></pre>

<p>each <code>identifier</code> match produces a <code>terminal_node</code> object, <i>not</i> an <code>identifier_node</code> object.</p>

<p>A level 1 warning is issued each time an &quot;autoaction&quot; is added to some production.</p>

<h2 id="Autotrees">Autotrees</h2>

<p>A commonly needed autoaction is one that builds a parse-tree. It is moderately tricky to set up such an action (which must treat terminals differently from non-terminals), so Parse::RecDescent simplifies the process by providing the <code>&lt;autotree&gt;</code> directive.</p>

<p>If this directive appears at the start of grammar, it causes Parse::RecDescent to insert autoactions at the end of any rule except those which already end in an action. The action inserted depends on whether the production is an intermediate rule (two or more items), or a terminal of the grammar (i.e. a single pattern or string item).</p>

<p>So, for example, the following grammar:</p>

<pre><code>    &lt;autotree&gt;

    file    : command(s)
    command : get | set | vet
    get : &#39;get&#39; ident &#39;;&#39;
    set : &#39;set&#39; ident &#39;to&#39; value &#39;;&#39;
    vet : &#39;check&#39; ident &#39;is&#39; value &#39;;&#39;
    ident   : /\w+/
    value   : /\d+/</code></pre>

<p>is equivalent to:</p>

<pre><code>    file    : command(s)        { bless \%item, $item[0] }
    command : get       { bless \%item, $item[0] }
    | set           { bless \%item, $item[0] }
    | vet           { bless \%item, $item[0] }
    get : &#39;get&#39; ident &#39;;&#39;   { bless \%item, $item[0] }
    set : &#39;set&#39; ident &#39;to&#39; value &#39;;&#39;    { bless \%item, $item[0] }
    vet : &#39;check&#39; ident &#39;is&#39; value &#39;;&#39;  { bless \%item, $item[0] }

    ident   : /\w+/  { bless {__VALUE__=&gt;$item[1]}, $item[0] }
    value   : /\d+/  { bless {__VALUE__=&gt;$item[1]}, $item[0] }</code></pre>

<p>Note that each node in the tree is blessed into a class of the same name as the rule itself. This makes it easy to build object-oriented processors for the parse-trees that the grammar produces. Note too that the last two rules produce special objects with the single attribute &#39;__VALUE__&#39;. This is because they consist solely of a single terminal.</p>

<p>This autoaction-ed grammar would then produce a parse tree in a data structure like this:</p>

<pre><code>    {
      file =&gt; {
        command =&gt; {
         [ get =&gt; {
            identifier =&gt; { __VALUE__ =&gt; &#39;a&#39; },
              },
           set =&gt; {
            identifier =&gt; { __VALUE__ =&gt; &#39;b&#39; },
            value      =&gt; { __VALUE__ =&gt; &#39;7&#39; },
              },
           vet =&gt; {
            identifier =&gt; { __VALUE__ =&gt; &#39;b&#39; },
            value      =&gt; { __VALUE__ =&gt; &#39;7&#39; },
              },
          ],
           },
      }
    }</code></pre>

<p>(except, of course, that each nested hash would also be blessed into the appropriate class).</p>

<p>You can also specify a base class for the <code>&lt;autotree&gt;</code> directive. The supplied prefix will be prepended to the rule names when creating tree nodes. The following are equivalent:</p>

<pre><code>    &lt;autotree:MyBase::Class&gt;
    &lt;autotree:MyBase::Class::&gt;</code></pre>

<p>And will produce a root node blessed into the <code>MyBase::Class::file</code> package in the example above.</p>

<h2 id="Autostubbing">Autostubbing</h2>

<p>Normally, if a subrule appears in some production, but no rule of that name is ever defined in the grammar, the production which refers to the non-existent subrule fails immediately. This typically occurs as a result of misspellings, and is a sufficiently common occurrence that a warning is generated for such situations.</p>

<p>However, when prototyping a grammar it is sometimes useful to be able to use subrules before a proper specification of them is really possible. For example, a grammar might include a section like:</p>

<pre><code>    function_call: identifier &#39;(&#39; arg(s?) &#39;)&#39;

    identifier: /[a-z]\w*/i</code></pre>

<p>where the possible format of an argument is sufficiently complex that it is not worth specifying in full until the general function call syntax has been debugged. In this situation it is convenient to leave the real rule <code>arg</code> undefined and just slip in a placeholder (or &quot;stub&quot;):</p>

<pre><code>    arg: &#39;arg&#39;</code></pre>

<p>so that the function call syntax can be tested with dummy input such as:</p>

<pre><code>    f0()
    f1(arg)
    f2(arg arg)
    f3(arg arg arg)</code></pre>

<p>et cetera.</p>

<p>Early in prototyping, many such &quot;stubs&quot; may be required, so <code>Parse::RecDescent</code> provides a means of automating their definition. If the variable <code>$::RD_AUTOSTUB</code> is defined when a parser is built, a subrule reference to any non-existent rule (say, <code>subrule</code>), will cause a &quot;stub&quot; rule to be automatically defined in the generated parser. If <code>$::RD_AUTOSTUB eq &#39;1&#39;</code> or is false, a stub rule of the form:</p>

<pre><code>    subrule: &#39;subrule&#39;</code></pre>

<p>will be generated. The special-case for a value of <code>&#39;1&#39;</code> is to allow the use of the <b>perl -s</b> with <b>-RD_AUTOSTUB</b> without generating <code>subrule: &#39;1&#39;</code> per below. If <code>$::RD_AUTOSTUB</code> is true, a stub rule of the form:</p>

<pre><code>    subrule: $::RD_AUTOSTUB</code></pre>

<p>will be generated. <code>$::RD_AUTOSTUB</code> must contain a valid production item, no checking is performed. No lazy evaluation of <code>$::RD_AUTOSTUB</code> is performed, it is evaluated at the time the Parser is generated.</p>

<p>Hence, with <code>$::RD_AUTOSTUB</code> defined, it is possible to only partially specify a grammar, and then &quot;fake&quot; matches of the unspecified (sub)rules by just typing in their name, or a literal value that was assigned to <code>$::RD_AUTOSTUB</code>.</p>

<h2 id="Look-ahead"><a id="Look"></a>Look-ahead</h2>

<p>If a subrule, token, or action is prefixed by &quot;...&quot;, then it is treated as a &quot;look-ahead&quot; request. That means that the current production can (as usual) only succeed if the specified item is matched, but that the matching <i>does not consume any of the text being parsed</i>. This is very similar to the <code>/(?=...)/</code> look-ahead construct in Perl patterns. Thus, the rule:</p>

<pre><code>    inner_word: word ...word</code></pre>

<p>will match whatever the subrule &quot;word&quot; matches, provided that match is followed by some more text which subrule &quot;word&quot; would also match (although this second substring is not actually consumed by &quot;inner_word&quot;)</p>

<p>Likewise, a &quot;...!&quot; prefix, causes the following item to succeed (without consuming any text) if and only if it would normally fail. Hence, a rule such as:</p>

<pre><code>    identifier: ...!keyword ...!&#39;_&#39; /[A-Za-z_]\w*/</code></pre>

<p>matches a string of characters which satisfies the pattern <code>/[A-Za-z_]\w*/</code>, but only if the same sequence of characters would not match either subrule &quot;keyword&quot; or the literal token &#39;_&#39;.</p>

<p>Sequences of look-ahead prefixes accumulate, multiplying their positive and/or negative senses. Hence:</p>

<pre><code>    inner_word: word ...!......!word</code></pre>

<p>is exactly equivalent to the original example above (a warning is issued in cases like these, since they often indicate something left out, or misunderstood).</p>

<p>Note that actions can also be treated as look-aheads. In such cases, the state of the parser text (in the local variable <code>$text</code>) <i>after</i> the look-ahead action is guaranteed to be identical to its state <i>before</i> the action, regardless of how it&#39;s changed <i>within</i> the action (unless you actually undefine <code>$text</code>, in which case you get the disaster you deserve :-).</p>

<h2 id="Directives">Directives</h2>

<p>Directives are special pre-defined actions which may be used to alter the behaviour of the parser. There are currently twenty-three directives: <code>&lt;commit&gt;</code>, <code>&lt;uncommit&gt;</code>, <code>&lt;reject&gt;</code>, <code>&lt;score&gt;</code>, <code>&lt;autoscore&gt;</code>, <code>&lt;skip&gt;</code>, <code>&lt;resync&gt;</code>, <code>&lt;error&gt;</code>, <code>&lt;warn&gt;</code>, <code>&lt;hint&gt;</code>, <code>&lt;trace_build&gt;</code>, <code>&lt;trace_parse&gt;</code>, <code>&lt;nocheck&gt;</code>, <code>&lt;rulevar&gt;</code>, <code>&lt;matchrule&gt;</code>, <code>&lt;leftop&gt;</code>, <code>&lt;rightop&gt;</code>, <code>&lt;defer&gt;</code>, <code>&lt;nocheck&gt;</code>, <code>&lt;perl_quotelike&gt;</code>, <code>&lt;perl_codeblock&gt;</code>, <code>&lt;perl_variable&gt;</code>, and <code>&lt;token&gt;</code>.</p>

<dl>

<dt id="Committing-and-uncommitting"><a id="Committing"></a>Committing and uncommitting</dt>
<dd>

<p>The <code>&lt;commit&gt;</code> and <code>&lt;uncommit&gt;</code> directives permit the recursive descent of the parse tree to be pruned (or &quot;cut&quot;) for efficiency. Within a rule, a <code>&lt;commit&gt;</code> directive instructs the rule to ignore subsequent productions if the current production fails. For example:</p>

<pre><code>    command: &#39;find&#39; &lt;commit&gt; filename
       | &#39;open&#39; &lt;commit&gt; filename
       | &#39;move&#39; filename filename</code></pre>

<p>Clearly, if the leading token &#39;find&#39; is matched in the first production but that production fails for some other reason, then the remaining productions cannot possibly match. The presence of the <code>&lt;commit&gt;</code> causes the &quot;command&quot; rule to fail immediately if an invalid &quot;find&quot; command is found, and likewise if an invalid &quot;open&quot; command is encountered.</p>

<p>It is also possible to revoke a previous commitment. For example:</p>

<pre><code>    if_statement: &#39;if&#39; &lt;commit&gt; condition
        &#39;then&#39; block &lt;uncommit&gt;
        &#39;else&#39; block
        | &#39;if&#39; &lt;commit&gt; condition
        &#39;then&#39; block</code></pre>

<p>In this case, a failure to find an &quot;else&quot; block in the first production shouldn&#39;t preclude trying the second production, but a failure to find a &quot;condition&quot; certainly should.</p>

<p>As a special case, any production in which the <i>first</i> item is an <code>&lt;uncommit&gt;</code> immediately revokes a preceding <code>&lt;commit&gt;</code> (even though the production would not otherwise have been tried). For example, in the rule:</p>

<pre><code>    request: &#39;explain&#39; expression
           | &#39;explain&#39; &lt;commit&gt; keyword
           | &#39;save&#39;
           | &#39;quit&#39;
           | &lt;uncommit&gt; term &#39;?&#39;</code></pre>

<p>if the text being matched was &quot;explain?&quot;, and the first two productions failed, then the <code>&lt;commit&gt;</code> in production two would cause productions three and four to be skipped, but the leading <code>&lt;uncommit&gt;</code> in the production five would allow that production to attempt a match.</p>

<p>Note in the preceding example, that the <code>&lt;commit&gt;</code> was only placed in production two. If production one had been:</p>

<pre><code>    request: &#39;explain&#39; &lt;commit&gt; expression</code></pre>

<p>then production two would be (inappropriately) skipped if a leading &quot;explain...&quot; was encountered.</p>

<p>Both <code>&lt;commit&gt;</code> and <code>&lt;uncommit&gt;</code> directives always succeed, and their value is always 1.</p>

</dd>
<dt id="Rejecting-a-production"><a id="Rejecting"></a>Rejecting a production</dt>
<dd>

<p>The <code>&lt;reject&gt;</code> directive immediately causes the current production to fail (it is exactly equivalent to, but more obvious than, the action <code>{undef}</code>). A <code>&lt;reject&gt;</code> is useful when it is desirable to get the side effects of the actions in one production, without prejudicing a match by some other production later in the rule. For example, to insert tracing code into the parse:</p>

<pre><code>    complex_rule: { print &quot;In complex rule...\n&quot;; } &lt;reject&gt;

    complex_rule: simple_rule &#39;+&#39; &#39;i&#39; &#39;*&#39; simple_rule
        | &#39;i&#39; &#39;*&#39; simple_rule
        | simple_rule</code></pre>

<p>It is also possible to specify a conditional rejection, using the form <code>&lt;reject:<i>condition</i>&gt;</code>, which only rejects if the specified condition is true. This form of rejection is exactly equivalent to the action <code>{(<i>condition</i>)?undef:1}&gt;</code>. For example:</p>

<pre><code>    command: save_command
       | restore_command
       | &lt;reject: defined $::tolerant&gt; { exit }
       | &lt;error: Unknown command. Ignored.&gt;</code></pre>

<p>A <code>&lt;reject&gt;</code> directive never succeeds (and hence has no associated value). A conditional rejection may succeed (if its condition is not satisfied), in which case its value is 1.</p>

<p>As an extra optimization, <code>Parse::RecDescent</code> ignores any production which <i>begins</i> with an unconditional <code>&lt;reject&gt;</code> directive, since any such production can never successfully match or have any useful side-effects. A level 1 warning is issued in all such cases.</p>

<p>Note that productions beginning with conditional <code>&lt;reject:...&gt;</code> directives are <i>never</i> &quot;optimized away&quot; in this manner, even if they are always guaranteed to fail (for example: <code>&lt;reject:1&gt;</code>)</p>

<p>Due to the way grammars are parsed, there is a minor restriction on the condition of a conditional <code>&lt;reject:...&gt;</code>: it cannot contain any raw &#39;&lt;&#39; or &#39;&gt;&#39; characters. For example:</p>

<pre><code>    line: cmd &lt;reject: $thiscolumn &gt; max&gt; data</code></pre>

<p>results in an error when a parser is built from this grammar (since the grammar parser has no way of knowing whether the first &gt; is a &quot;less than&quot; or the end of the <code>&lt;reject:...&gt;</code>.</p>

<p>To overcome this problem, put the condition inside a do{} block:</p>

<pre><code>    line: cmd &lt;reject: do{$thiscolumn &gt; max}&gt; data</code></pre>

<p>Note that the same problem may occur in other directives that take arguments. The same solution will work in all cases.</p>

</dd>
<dt id="Skipping-between-terminals"><a id="Skipping"></a>Skipping between terminals</dt>
<dd>

<p>The <code>&lt;skip&gt;</code> directive enables the terminal prefix used in a production to be changed. For example:</p>

<pre><code>    OneLiner: Command &lt;skip:&#39;[ \t]*&#39;&gt; Arg(s) /;/</code></pre>

<p>causes only blanks and tabs to be skipped before terminals in the <code>Arg</code> subrule (and any of <i>its</i> subrules&gt;, and also before the final <code>/;/</code> terminal. Once the production is complete, the previous terminal prefix is reinstated. Note that this implies that distinct productions of a rule must reset their terminal prefixes individually.</p>

<p>The <code>&lt;skip&gt;</code> directive evaluates to the <i>previous</i> terminal prefix, so it&#39;s easy to reinstate a prefix later in a production:</p>

<pre><code>    Command: &lt;skip:&quot;,&quot;&gt; CSV(s) &lt;skip:$item[1]&gt; Modifier</code></pre>

<p>The value specified after the colon is interpolated into a pattern, so all of the following are equivalent (though their efficiency increases down the list):</p>

<pre><code>    &lt;skip: &quot;$colon|$comma&quot;&gt;   # ASSUMING THE VARS HOLD THE OBVIOUS VALUES

    &lt;skip: &#39;:|,&#39;&gt;

    &lt;skip: q{[:,]}&gt;

    &lt;skip: qr/[:,]/&gt;</code></pre>

<p>There is no way of directly setting the prefix for an entire rule, except as follows:</p>

<pre><code>    Rule: &lt;skip: &#39;[ \t]*&#39;&gt; Prod1
        | &lt;skip: &#39;[ \t]*&#39;&gt; Prod2a Prod2b
        | &lt;skip: &#39;[ \t]*&#39;&gt; Prod3</code></pre>

<p>or, better:</p>

<pre><code>    Rule: &lt;skip: &#39;[ \t]*&#39;&gt;
    (
        Prod1
      | Prod2a Prod2b
      | Prod3
    )</code></pre>

<p>The skip pattern is passed down to subrules, so setting the skip for the top-level rule as described above actually sets the prefix for the entire grammar (provided that you only call the method corresponding to the top-level rule itself). Alternatively, or if you have more than one top-level rule in your grammar, you can provide a global <code>&lt;skip&gt;</code> directive prior to defining any rules in the grammar. These are the preferred alternatives to setting <code>$Parse::RecDescent::skip</code>.</p>

<p>Additionally, using <code>&lt;skip&gt;</code> actually allows you to have a completely dynamic skipping behaviour. For example:</p>

<pre><code>   Rule_with_dynamic_skip: &lt;skip: $::skip_pattern&gt; Rule</code></pre>

<p>Then you can set <code>$::skip_pattern</code> before invoking <code>Rule_with_dynamic_skip</code> and have it skip whatever you specified.</p>

<p><b>Note: Up to release 1.51 of Parse::RecDescent, an entirely different mechanism was used for specifying terminal prefixes. The current method is not backwards-compatible with that early approach. The current approach is stable and will not change again.</b></p>

<p><b>Note: the global <code>&lt;skip&gt;</code> directive added in 1.967_004 did not interpolate the pattern argument, instead the pattern was placed inside of single quotes and then interpolated. This behavior was changed in 1.967_010 so that all <code>&lt;skip&gt;</code> directives behavior similarly.</b></p>

</dd>
<dt id="Resynchronization">Resynchronization</dt>
<dd>

<p>The <code>&lt;resync&gt;</code> directive provides a visually distinctive means of consuming some of the text being parsed, usually to skip an erroneous input. In its simplest form <code>&lt;resync&gt;</code> simply consumes text up to and including the next newline (<code>&quot;\n&quot;</code>) character, succeeding only if the newline is found, in which case it causes its surrounding rule to return zero on success.</p>

<p>In other words, a <code>&lt;resync&gt;</code> is exactly equivalent to the token <code>/[^\n]*\n/</code> followed by the action <span style="white-space: nowrap;"><code>{ $return = 0 }</code></span> (except that productions beginning with a <code>&lt;resync&gt;</code> are ignored when generating error messages). A typical use might be:</p>

<pre><code>    script : command(s)

    command: save_command
       | restore_command
       | &lt;resync&gt; # TRY NEXT LINE, IF POSSIBLE</code></pre>

<p>It is also possible to explicitly specify a resynchronization pattern, using the <code>&lt;resync:<i>pattern</i>&gt;</code> variant. This version succeeds only if the specified pattern matches (and consumes) the parsed text. In other words, <code>&lt;resync:<i>pattern</i>&gt;</code> is exactly equivalent to the token <code>/<i>pattern</i>/</code> (followed by a <span style="white-space: nowrap;"><code>{ $return = 0 }</code></span> action). For example, if commands were terminated by newlines or semi-colons:</p>

<pre><code>    command: save_command
       | restore_command
       | &lt;resync:[^;\n]*[;\n]&gt;</code></pre>

<p>The value of a successfully matched <code>&lt;resync&gt;</code> directive (of either type) is the text that it consumed. Note, however, that since the directive also sets <code>$return</code>, a production consisting of a lone <code>&lt;resync&gt;</code> succeeds but returns the value zero (which a calling rule may find useful to distinguish between &quot;true&quot; matches and &quot;tolerant&quot; matches). Remember that returning a zero value indicates that the rule <i>succeeded</i> (since only an <code>undef</code> denotes failure within <code>Parse::RecDescent</code> parsers.</p>

</dd>
<dt id="Error-handling"><a id="Error"></a>Error handling</dt>
<dd>

<p>The <code>&lt;error&gt;</code> directive provides automatic or user-defined generation of error messages during a parse. In its simplest form <code>&lt;error&gt;</code> prepares an error message based on the mismatch between the last item expected and the text which cause it to fail. For example, given the rule:</p>

<pre><code>    McCoy: curse &#39;,&#39; name &#39;, I&#39;m a doctor, not a&#39; a_profession &#39;!&#39;
     | pronoun &#39;dead,&#39; name &#39;!&#39;
     | &lt;error&gt;</code></pre>

<p>the following strings would produce the following messages:</p>

<dl>

<dt id="&quot;Amen,-Jim!&quot;"><a id="Amen--Jim"></a>&quot;Amen, Jim!&quot;</dt>
<dd>

<pre><code>       ERROR (line 1): Invalid McCoy: Expected curse or pronoun
           not found</code></pre>

</dd>
<dt id="&quot;Dammit,-Jim,-I&#39;m-a-doctor!&quot;"><a id="Dammit--Jim--I-m-a-doctor"></a>&quot;Dammit, Jim, I&#39;m a doctor!&quot;</dt>
<dd>

<pre><code>       ERROR (line 1): Invalid McCoy: Expected &quot;, I&#39;m a doctor, not a&quot;
           but found &quot;, I&#39;m a doctor!&quot; instead</code></pre>

</dd>
<dt id="&quot;He&#39;s-dead,\n&quot;"><a id="He-s-dead-n"></a>&quot;He&#39;s dead,\n&quot;</dt>
<dd>

<pre><code>       ERROR (line 2): Invalid McCoy: Expected name not found</code></pre>

</dd>
<dt id="&quot;He&#39;s-alive!&quot;"><a id="He-s-alive"></a>&quot;He&#39;s alive!&quot;</dt>
<dd>

<pre><code>       ERROR (line 1): Invalid McCoy: Expected &#39;dead,&#39; but found
           &quot;alive!&quot; instead</code></pre>

</dd>
<dt id="&quot;Dammit,-Jim,-I&#39;m-a-doctor,-not-a-pointy-eared-Vulcan!&quot;"><a id="Dammit--Jim--I-m-a-doctor--not-a-pointy-eared-Vulcan"></a>&quot;Dammit, Jim, I&#39;m a doctor, not a pointy-eared Vulcan!&quot;</dt>
<dd>

<pre><code>       ERROR (line 1): Invalid McCoy: Expected a profession but found
           &quot;pointy-eared Vulcan!&quot; instead</code></pre>

</dd>
</dl>

<p>Note that, when autogenerating error messages, all underscores in any rule name used in a message are replaced by single spaces (for example &quot;a_production&quot; becomes &quot;a production&quot;). Judicious choice of rule names can therefore considerably improve the readability of automatic error messages (as well as the maintainability of the original grammar).</p>

<p>If the automatically generated error is not sufficient, it is possible to provide an explicit message as part of the error directive. For example:</p>

<pre><code>    Spock: &quot;Fascinating &#39;,&#39; (name | &#39;Captain&#39;) &#39;.&#39;
     | &quot;Highly illogical, doctor.&quot;
     | &lt;error: He never said that!&gt;</code></pre>

<p>which would result in <i>all</i> failures to parse a &quot;Spock&quot; subrule printing the following message:</p>

<pre><code>       ERROR (line &lt;N&gt;): Invalid Spock:  He never said that!</code></pre>

<p>The error message is treated as a &quot;qq{...}&quot; string and interpolated when the error is generated (<i>not</i> when the directive is specified!). Hence:</p>

<pre><code>    &lt;error: Mystical error near &quot;$text&quot;&gt;</code></pre>

<p>would correctly insert the ambient text string which caused the error.</p>

<p>There are two other forms of error directive: <code>&lt;error?&gt;</code> and <span style="white-space: nowrap;"><code>&lt;error?: msg&gt;</code></span>. These behave just like <code>&lt;error&gt;</code> and <span style="white-space: nowrap;"><code>&lt;error: msg&gt;</code></span> respectively, except that they are only triggered if the rule is &quot;committed&quot; at the time they are encountered. For example:</p>

<pre><code>    Scotty: &quot;Ya kenna change the Laws of Phusics,&quot; &lt;commit&gt; name
      | name &lt;commit&gt; &#39;,&#39; &#39;she&#39;s goanta blaw!&#39;
      | &lt;error?&gt;</code></pre>

<p>will only generate an error for a string beginning with &quot;Ya kenna change the Laws o&#39; Phusics,&quot; or a valid name, but which still fails to match the corresponding production. That is, <code>$parser-&gt;Scotty(&quot;Aye, Cap&#39;ain&quot;)</code> will fail silently (since neither production will &quot;commit&quot; the rule on that input), whereas <span style="white-space: nowrap;"><code>$parser-&gt;Scotty(&quot;Mr Spock, ah jest kenna do&#39;ut!&quot;)</code></span> will fail with the error message:</p>

<pre><code>       ERROR (line 1): Invalid Scotty: expected &#39;she&#39;s goanta blaw!&#39;
           but found &#39;I jest kenna do&#39;ut!&#39; instead.</code></pre>

<p>since in that case the second production would commit after matching the leading name.</p>

<p>Note that to allow this behaviour, all <code>&lt;error&gt;</code> directives which are the first item in a production automatically uncommit the rule just long enough to allow their production to be attempted (that is, when their production fails, the commitment is reinstated so that subsequent productions are skipped).</p>

<p>In order to <i>permanently</i> uncommit the rule before an error message, it is necessary to put an explicit <code>&lt;uncommit&gt;</code> before the <code>&lt;error&gt;</code>. For example:</p>

<pre><code>    line: &#39;Kirk:&#39;  &lt;commit&gt; Kirk
    | &#39;Spock:&#39; &lt;commit&gt; Spock
    | &#39;McCoy:&#39; &lt;commit&gt; McCoy
    | &lt;uncommit&gt; &lt;error?&gt; &lt;reject&gt;
    | &lt;resync&gt;</code></pre>

<p>Error messages generated by the various <code>&lt;error...&gt;</code> directives are not displayed immediately. Instead, they are &quot;queued&quot; in a buffer and are only displayed once parsing ultimately fails. Moreover, <code>&lt;error...&gt;</code> directives that cause one production of a rule to fail are automatically removed from the message queue if another production subsequently causes the entire rule to succeed. This means that you can put <code>&lt;error...&gt;</code> directives wherever useful diagnosis can be done, and only those associated with actual parser failure will ever be displayed. Also see <a href="./Parse::RecDescent.html#GOTCHAS">&quot;GOTCHAS&quot;</a>.</p>

<p>As a general rule, the most useful diagnostics are usually generated either at the very lowest level within the grammar, or at the very highest. A good rule of thumb is to identify those subrules which consist mainly (or entirely) of terminals, and then put an <code>&lt;error...&gt;</code> directive at the end of any other rule which calls one or more of those subrules.</p>

<p>There is one other situation in which the output of the various types of error directive is suppressed; namely, when the rule containing them is being parsed as part of a &quot;look-ahead&quot; (see <a href="./Parse::RecDescent.html#Look-ahead">&quot;Look-ahead&quot;</a>). In this case, the error directive will still cause the rule to fail, but will do so silently.</p>

<p>An unconditional <code>&lt;error&gt;</code> directive always fails (and hence has no associated value). This means that encountering such a directive always causes the production containing it to fail. Hence an <code>&lt;error&gt;</code> directive will inevitably be the last (useful) item of a rule (a level 3 warning is issued if a production contains items after an unconditional <code>&lt;error&gt;</code> directive).</p>

<p>An <code>&lt;error?&gt;</code> directive will <i>succeed</i> (that is: fail to fail :-), if the current rule is uncommitted when the directive is encountered. In that case the directive&#39;s associated value is zero. Hence, this type of error directive <i>can</i> be used before the end of a production. For example:</p>

<pre><code>    command: &#39;do&#39; &lt;commit&gt; something
       | &#39;report&#39; &lt;commit&gt; something
       | &lt;error?: Syntax error&gt; &lt;error: Unknown command&gt;</code></pre>

<p><b>Warning:</b> The <code>&lt;error?&gt;</code> directive does <i>not</i> mean &quot;always fail (but do so silently unless committed)&quot;. It actually means &quot;only fail (and report) if committed, otherwise <i>succeed</i>&quot;. To achieve the &quot;fail silently if uncommitted&quot; semantics, it is necessary to use:</p>

<pre><code>    rule: item &lt;commit&gt; item(s)
    | &lt;error?&gt; &lt;reject&gt;  # FAIL SILENTLY UNLESS COMMITTED</code></pre>

<p>However, because people seem to expect a lone <code>&lt;error?&gt;</code> directive to work like this:</p>

<pre><code>    rule: item &lt;commit&gt; item(s)
    | &lt;error?: Error message if committed&gt;
    | &lt;error:  Error message if uncommitted&gt;</code></pre>

<p>Parse::RecDescent automatically appends a <code>&lt;reject&gt;</code> directive if the <code>&lt;error?&gt;</code> directive is the only item in a production. A level 2 warning (see below) is issued when this happens.</p>

<p>The level of error reporting during both parser construction and parsing is controlled by the presence or absence of four global variables: <code>$::RD_ERRORS</code>, <code>$::RD_WARN</code>, <code>$::RD_HINT</code>, and &lt;$::RD_TRACE&gt;. If <code>$::RD_ERRORS</code> is defined (and, by default, it is) then fatal errors are reported.</p>

<p>Whenever <code>$::RD_WARN</code> is defined, certain non-fatal problems are also reported.</p>

<p>Warnings have an associated &quot;level&quot;: 1, 2, or 3. The higher the level, the more serious the warning. The value of the corresponding global variable (<code>$::RD_WARN</code>) determines the <i>lowest</i> level of warning to be displayed. Hence, to see <i>all</i> warnings, set <code>$::RD_WARN</code> to 1. To see only the most serious warnings set <code>$::RD_WARN</code> to 3. By default <code>$::RD_WARN</code> is initialized to 3, ensuring that serious but non-fatal errors are automatically reported.</p>

<p>There is also a grammar directive to turn on warnings from within the grammar: <code>&lt;warn&gt;</code>. It takes an optional argument, which specifies the warning level: <code>&lt;warn: 2&gt;</code>.</p>

<p>See <i>&quot;DIAGNOSTICS&quot;</i> for a list of the various error and warning messages that Parse::RecDescent generates when these two variables are defined.</p>

<p>Defining any of the remaining variables (which are not defined by default) further increases the amount of information reported. Defining <code>$::RD_HINT</code> causes the parser generator to offer more detailed analyses and hints on both errors and warnings. Note that setting <code>$::RD_HINT</code> at any point automagically sets <code>$::RD_WARN</code> to 1. There is also a <code>&lt;hint&gt;</code> directive, which can be hard-coded into a grammar.</p>

<p>Defining <code>$::RD_TRACE</code> causes the parser generator and the parser to report their progress to STDERR in excruciating detail (although, without hints unless $::RD_HINT is separately defined). This detail can be moderated in only one respect: if <code>$::RD_TRACE</code> has an integer value (<i>N</i>) greater than 1, only the <i>N</i> characters of the &quot;current parsing context&quot; (that is, where in the input string we are at any point in the parse) is reported at any time.</p>

<p><code>$::RD_TRACE</code> is mainly useful for debugging a grammar that isn&#39;t behaving as you expected it to. To this end, if <code>$::RD_TRACE</code> is defined when a parser is built, any actual parser code which is generated is also written to a file named &quot;RD_TRACE&quot; in the local directory.</p>

<p>There are two directives associated with the <code>$::RD_TRACE</code> variable. If a grammar contains a <code>&lt;trace_build&gt;</code> directive anywhere in its specification, <code>$::RD_TRACE</code> is turned on during the parser construction phase. If a grammar contains a <code>&lt;trace_parse&gt;</code> directive anywhere in its specification, <code>$::RD_TRACE</code> is turned on during any parse the parser performs.</p>

<p>Note that the four variables belong to the &quot;main&quot; package, which makes them easier to refer to in the code controlling the parser, and also makes it easy to turn them into command line flags (&quot;-RD_ERRORS&quot;, &quot;-RD_WARN&quot;, &quot;-RD_HINT&quot;, &quot;-RD_TRACE&quot;) under <b>perl -s</b>.</p>

<p>The corresponding directives are useful to &quot;hardwire&quot; the various debugging features into a particular grammar (rather than having to set and reset external variables).</p>

</dd>
<dt id="Redirecting-diagnostics"><a id="Redirecting"></a>Redirecting diagnostics</dt>
<dd>

<p>The diagnostics provided by the tracing mechanism always go to STDERR. If you need them to go elsewhere, localize and reopen STDERR prior to the parse.</p>

<p>For example:</p>

<pre><code>    {
        local *STDERR = IO::File-&gt;new(&quot;&gt;$filename&quot;) or die $!;

        my $result = $parser-&gt;startrule($text);
    }</code></pre>

</dd>
<dt id="Consistency-checks"><a id="Consistency"></a>Consistency checks</dt>
<dd>

<p>Whenever a parser is build, Parse::RecDescent carries out a number of (potentially expensive) consistency checks. These include: verifying that the grammar is not left-recursive and that no rules have been left undefined.</p>

<p>These checks are important safeguards during development, but unnecessary overheads when the grammar is stable and ready to be deployed. So Parse::RecDescent provides a directive to disable them: <code>&lt;nocheck&gt;</code>.</p>

<p>If a grammar contains a <code>&lt;nocheck&gt;</code> directive anywhere in its specification, the extra compile-time checks are by-passed.</p>

</dd>
<dt id="Specifying-local-variables"><a id="Specifying"></a>Specifying local variables</dt>
<dd>

<p>It is occasionally convenient to specify variables which are local to a single rule. This may be achieved by including a <code>&lt;rulevar:...&gt;</code> directive anywhere in the rule. For example:</p>

<pre><code>    markup: &lt;rulevar: $tag&gt;

    markup: tag {($tag=$item[1]) =~ s/^&lt;|&gt;$//g} body[$tag]</code></pre>

<p>The example <code>&lt;rulevar: $tag&gt;</code> directive causes a &quot;my&quot; variable named <code>$tag</code> to be declared at the start of the subroutine implementing the <code>markup</code> rule (that is, <i>before</i> the first production, regardless of where in the rule it is specified).</p>

<p>Specifically, any directive of the form: <code>&lt;rulevar:<i>text</i>&gt;</code> causes a line of the form <code>my <i>text</i>;</code> to be added at the beginning of the rule subroutine, immediately after the definitions of the following local variables:</p>

<pre><code>    $thisparser $commit
    $thisrule   @item
    $thisline   @arg
    $text   %arg</code></pre>

<p>This means that the following <code>&lt;rulevar&gt;</code> directives work as expected:</p>

<pre><code>    &lt;rulevar: $count = 0 &gt;

    &lt;rulevar: $firstarg = $arg[0] || &#39;&#39; &gt;

    &lt;rulevar: $myItems = \@item &gt;

    &lt;rulevar: @context = ( $thisline, $text, @arg ) &gt;

    &lt;rulevar: ($name,$age) = $arg{&quot;name&quot;,&quot;age&quot;} &gt;</code></pre>

<p>If a variable that is also visible to subrules is required, it needs to be <code>local</code>&#39;d, not <code>my</code>&#39;d. <code>rulevar</code> defaults to <code>my</code>, but if <code>local</code> is explicitly specified:</p>

<pre><code>    &lt;rulevar: local $count = 0 &gt;</code></pre>

<p>then a <code>local</code>-ized variable is declared instead, and will be available within subrules.</p>

<p>Note however that, because all such variables are &quot;my&quot; variables, their values <i>do not persist</i> between match attempts on a given rule. To preserve values between match attempts, values can be stored within the &quot;local&quot; member of the <code>$thisrule</code> object:</p>

<pre><code>    countedrule: { $thisrule-&gt;{&quot;local&quot;}{&quot;count&quot;}++ }
         &lt;reject&gt;
       | subrule1
       | subrule2
       | &lt;reject: $thisrule-&gt;{&quot;local&quot;}{&quot;count&quot;} == 1&gt;
         subrule3</code></pre>

<p>When matching a rule, each <code>&lt;rulevar&gt;</code> directive is matched as if it were an unconditional <code>&lt;reject&gt;</code> directive (that is, it causes any production in which it appears to immediately fail to match). For this reason (and to improve readability) it is usual to specify any <code>&lt;rulevar&gt;</code> directive in a separate production at the start of the rule (this has the added advantage that it enables <code>Parse::RecDescent</code> to optimize away such productions, just as it does for the <code>&lt;reject&gt;</code> directive).</p>

</dd>
<dt id="Dynamically-matched-rules"><a id="Dynamically"></a>Dynamically matched rules</dt>
<dd>

<p>Because regexes and double-quoted strings are interpolated, it is relatively easy to specify productions with &quot;context sensitive&quot; tokens. For example:</p>

<pre><code>    command:  keyword  body  &quot;end $item[1]&quot;</code></pre>

<p>which ensures that a command block is bounded by a &quot;<i>&lt;keyword&gt;</i>...end <i>&lt;same keyword&gt;</i>&quot; pair.</p>

<p>Building productions in which subrules are context sensitive is also possible, via the <code>&lt;matchrule:...&gt;</code> directive. This directive behaves identically to a subrule item, except that the rule which is invoked to match it is determined by the string specified after the colon. For example, we could rewrite the <code>command</code> rule like this:</p>

<pre><code>    command:  keyword  &lt;matchrule:body&gt;  &quot;end $item[1]&quot;</code></pre>

<p>Whatever appears after the colon in the directive is treated as an interpolated string (that is, as if it appeared in <code>qq{...}</code> operator) and the value of that interpolated string is the name of the subrule to be matched.</p>

<p>Of course, just putting a constant string like <code>body</code> in a <code>&lt;matchrule:...&gt;</code> directive is of little interest or benefit. The power of directive is seen when we use a string that interpolates to something interesting. For example:</p>

<pre><code>    command:    keyword &lt;matchrule:$item[1]_body&gt; &quot;end $item[1]&quot;

    keyword:    &#39;while&#39; | &#39;if&#39; | &#39;function&#39;

    while_body: condition block

    if_body:    condition block (&#39;else&#39; block)(?)

    function_body:  arglist block</code></pre>

<p>Now the <code>command</code> rule selects how to proceed on the basis of the keyword that is found. It is as if <code>command</code> were declared:</p>

<pre><code>    command:    &#39;while&#39;    while_body    &quot;end while&quot;
       |    &#39;if&#39;       if_body   &quot;end if&quot;
       |    &#39;function&#39; function_body &quot;end function&quot;</code></pre>

<p>When a <code>&lt;matchrule:...&gt;</code> directive is used as a repeated subrule, the rule name expression is &quot;late-bound&quot;. That is, the name of the rule to be called is re-evaluated <i>each time</i> a match attempt is made. Hence, the following grammar:</p>

<pre><code>    { $::species = &#39;dogs&#39; }

    pair:   &#39;two&#39; &lt;matchrule:$::species&gt;(s)

    dogs:   /dogs/ { $::species = &#39;cats&#39; }

    cats:   /cats/</code></pre>

<p>will match the string &quot;two dogs cats cats&quot; completely, whereas it will only match the string &quot;two dogs dogs dogs&quot; up to the eighth letter. If the rule name were &quot;early bound&quot; (that is, evaluated only the first time the directive is encountered in a production), the reverse behaviour would be expected.</p>

<p>Note that the <code>matchrule</code> directive takes a string that is to be treated as a rule name, <i>not</i> as a rule invocation. That is, it&#39;s like a Perl symbolic reference, not an <code>eval</code>. Just as you can say:</p>

<pre><code>    $subname = &#39;foo&#39;;

    # and later...

    &amp;{$foo}(@args);</code></pre>

<p>but not:</p>

<pre><code>    $subname = &#39;foo(@args)&#39;;

    # and later...

    &amp;{$foo};</code></pre>

<p>likewise you can say:</p>

<pre><code>    $rulename = &#39;foo&#39;;

    # and in the grammar...

    &lt;matchrule:$rulename&gt;[@args]</code></pre>

<p>but not:</p>

<pre><code>    $rulename = &#39;foo[@args]&#39;;

    # and in the grammar...

    &lt;matchrule:$rulename&gt;</code></pre>

</dd>
<dt id="Deferred-actions"><a id="Deferred"></a>Deferred actions</dt>
<dd>

<p>The <code>&lt;defer:...&gt;</code> directive is used to specify an action to be performed when (and only if!) the current production ultimately succeeds.</p>

<p>Whenever a <code>&lt;defer:...&gt;</code> directive appears, the code it specifies is converted to a closure (an anonymous subroutine reference) which is queued within the active parser object. Note that, because the deferred code is converted to a closure, the values of any &quot;local&quot; variable (such as <code>$text</code>, &lt;@item&gt;, etc.) are preserved until the deferred code is actually executed.</p>

<p>If the parse ultimately succeeds <i>and</i> the production in which the <code>&lt;defer:...&gt;</code> directive was evaluated formed part of the successful parse, then the deferred code is executed immediately before the parse returns. If however the production which queued a deferred action fails, or one of the higher-level rules which called that production fails, then the deferred action is removed from the queue, and hence is never executed.</p>

<p>For example, given the grammar:</p>

<pre><code>    sentence: noun trans noun
    | noun intrans

    noun:     &#39;the dog&#39;
        { print &quot;$item[1]\t(noun)\n&quot; }
    |     &#39;the meat&#39;
        { print &quot;$item[1]\t(noun)\n&quot; }

    trans:    &#39;ate&#39;
        { print &quot;$item[1]\t(transitive)\n&quot; }

    intrans:  &#39;ate&#39;
        { print &quot;$item[1]\t(intransitive)\n&quot; }
       |  &#39;barked&#39;
        { print &quot;$item[1]\t(intransitive)\n&quot; }</code></pre>

<p>then parsing the sentence <code>&quot;the dog ate&quot;</code> would produce the output:</p>

<pre><code>    the dog  (noun)
    ate  (transitive)
    the dog  (noun)
    ate  (intransitive)</code></pre>

<p>This is because, even though the first production of <code>sentence</code> ultimately fails, its initial subrules <code>noun</code> and <code>trans</code> do match, and hence they execute their associated actions. Then the second production of <code>sentence</code> succeeds, causing the actions of the subrules <code>noun</code> and <code>intrans</code> to be executed as well.</p>

<p>On the other hand, if the actions were replaced by <code>&lt;defer:...&gt;</code> directives:</p>

<pre><code>    sentence: noun trans noun
    | noun intrans

    noun:     &#39;the dog&#39;
        &lt;defer: print &quot;$item[1]\t(noun)\n&quot; &gt;
    |     &#39;the meat&#39;
        &lt;defer: print &quot;$item[1]\t(noun)\n&quot; &gt;

    trans:    &#39;ate&#39;
        &lt;defer: print &quot;$item[1]\t(transitive)\n&quot; &gt;

    intrans:  &#39;ate&#39;
        &lt;defer: print &quot;$item[1]\t(intransitive)\n&quot; &gt;
       |  &#39;barked&#39;
        &lt;defer: print &quot;$item[1]\t(intransitive)\n&quot; &gt;</code></pre>

<p>the output would be:</p>

<pre><code>    the dog  (noun)
    ate  (intransitive)</code></pre>

<p>since deferred actions are only executed if they were evaluated in a production which ultimately contributes to the successful parse.</p>

<p>In this case, even though the first production of <code>sentence</code> caused the subrules <code>noun</code> and <code>trans</code> to match, that production ultimately failed and so the deferred actions queued by those subrules were subsequently discarded. The second production then succeeded, causing the entire parse to succeed, and so the deferred actions queued by the (second) match of the <code>noun</code> subrule and the subsequent match of <code>intrans</code> <i>are</i> preserved and eventually executed.</p>

<p>Deferred actions provide a means of improving the performance of a parser, by only executing those actions which are part of the final parse-tree for the input data.</p>

<p>Alternatively, deferred actions can be viewed as a mechanism for building (and executing) a customized subroutine corresponding to the given input data, much in the same way that autoactions (see <a href="./Parse::RecDescent.html#Autoactions">&quot;Autoactions&quot;</a>) can be used to build a customized data structure for specific input.</p>

<p>Whether or not the action it specifies is ever executed, a <code>&lt;defer:...&gt;</code> directive always succeeds, returning the number of deferred actions currently queued at that point.</p>

</dd>
<dt id="Parsing-Perl"><a id="Parsing"></a>Parsing Perl</dt>
<dd>

<p>Parse::RecDescent provides limited support for parsing subsets of Perl, namely: quote-like operators, Perl variables, and complete code blocks.</p>

<p>The <code>&lt;perl_quotelike&gt;</code> directive can be used to parse any Perl quote-like operator: <code>&#39;a string&#39;</code>, <code>m/a pattern/</code>, <code>tr{ans}{lation}</code>, etc. It does this by calling Text::Balanced::quotelike().</p>

<p>If a quote-like operator is found, a reference to an array of eight elements is returned. Those elements are identical to the last eight elements returned by Text::Balanced::extract_quotelike() in an array context, namely:</p>

<dl>

<dt id="[0]"><a id="pod"></a>[0]</dt>
<dd>

<p>the name of the quotelike operator -- &#39;q&#39;, &#39;qq&#39;, &#39;m&#39;, &#39;s&#39;, &#39;tr&#39; -- if the operator was named; otherwise <code>undef</code>,</p>

</dd>
<dt id="[1]"><a id="pod1"></a>[1]</dt>
<dd>

<p>the left delimiter of the first block of the operation,</p>

</dd>
<dt id="[2]"><a id="pod2"></a>[2]</dt>
<dd>

<p>the text of the first block of the operation (that is, the contents of a quote, the regex of a match, or substitution or the target list of a translation),</p>

</dd>
<dt id="[3]"><a id="pod3"></a>[3]</dt>
<dd>

<p>the right delimiter of the first block of the operation,</p>

</dd>
<dt id="[4]"><a id="pod4"></a>[4]</dt>
<dd>

<p>the left delimiter of the second block of the operation if there is one (that is, if it is a <code>s</code>, <code>tr</code>, or <code>y</code>); otherwise <code>undef</code>,</p>

</dd>
<dt id="[5]"><a id="pod5"></a>[5]</dt>
<dd>

<p>the text of the second block of the operation if there is one (that is, the replacement of a substitution or the translation list of a translation); otherwise <code>undef</code>,</p>

</dd>
<dt id="[6]"><a id="pod6"></a>[6]</dt>
<dd>

<p>the right delimiter of the second block of the operation (if any); otherwise <code>undef</code>,</p>

</dd>
<dt id="[7]"><a id="pod7"></a>[7]</dt>
<dd>

<p>the trailing modifiers on the operation (if any); otherwise <code>undef</code>.</p>

</dd>
</dl>

<p>If a quote-like expression is not found, the directive fails with the usual <code>undef</code> value.</p>

<p>The <code>&lt;perl_variable&gt;</code> directive can be used to parse any Perl variable: $scalar, @array, %hash, $ref-&gt;{field}[$index], etc. It does this by calling Text::Balanced::extract_variable().</p>

<p>If the directive matches text representing a valid Perl variable specification, it returns that text. Otherwise it fails with the usual <code>undef</code> value.</p>

<p>The <code>&lt;perl_codeblock&gt;</code> directive can be used to parse curly-brace-delimited block of Perl code, such as: { $a = 1; f() =~ m/pat/; }. It does this by calling Text::Balanced::extract_codeblock().</p>

<p>If the directive matches text representing a valid Perl code block, it returns that text. Otherwise it fails with the usual <code>undef</code> value.</p>

<p>You can also tell it what kind of brackets to use as the outermost delimiters. For example:</p>

<pre><code>    arglist: &lt;perl_codeblock ()&gt;</code></pre>

<p>causes an arglist to match a perl code block whose outermost delimiters are <code>(...)</code> (rather than the default <code>{...}</code>).</p>

</dd>
<dt id="Constructing-tokens"><a id="Constructing"></a>Constructing tokens</dt>
<dd>

<p>Eventually, Parse::RecDescent will be able to parse tokenized input, as well as ordinary strings. In preparation for this joyous day, the <code>&lt;token:...&gt;</code> directive has been provided. This directive creates a token which will be suitable for input to a Parse::RecDescent parser (when it eventually supports tokenized input).</p>

<p>The text of the token is the value of the immediately preceding item in the production. A <code>&lt;token:...&gt;</code> directive always succeeds with a return value which is the hash reference that is the new token. It also sets the return value for the production to that hash ref.</p>

<p>The <code>&lt;token:...&gt;</code> directive makes it easy to build a Parse::RecDescent-compatible lexer in Parse::RecDescent:</p>

<pre><code>    my $lexer = new Parse::RecDescent q
    {
    lex:    token(s)

    token:  /a\b/          &lt;token:INDEF&gt;
         |  /the\b/        &lt;token:DEF&gt;
         |  /fly\b/        &lt;token:NOUN,VERB&gt;
         |  /[a-z]+/i { lc $item[1] }  &lt;token:ALPHA&gt;
         |  &lt;error: Unknown token&gt;

    };</code></pre>

<p>which will eventually be able to be used with a regular Parse::RecDescent grammar:</p>

<pre><code>    my $parser = new Parse::RecDescent q
    {
    startrule: subrule1 subrule 2

    # ETC...
    };</code></pre>

<p>either with a pre-lexing phase:</p>

<pre><code>    $parser-&gt;startrule( $lexer-&gt;lex($data) );</code></pre>

<p>or with a lex-on-demand approach:</p>

<pre><code>    $parser-&gt;startrule( sub{$lexer-&gt;token(\$data)} );</code></pre>

<p>But at present, only the <code>&lt;token:...&gt;</code> directive is actually implemented. The rest is vapourware.</p>

</dd>
<dt id="Specifying-operations"><a id="Specifying1"></a>Specifying operations</dt>
<dd>

<p>One of the commonest requirements when building a parser is to specify binary operators. Unfortunately, in a normal grammar, the rules for such things are awkward:</p>

<pre><code>    disjunction:    conjunction (&#39;or&#39; conjunction)(s?)
        { $return = [ $item[1], @{$item[2]} ] }

    conjunction:    atom (&#39;and&#39; atom)(s?)
        { $return = [ $item[1], @{$item[2]} ] }</code></pre>

<p>or inefficient:</p>

<pre><code>    disjunction:    conjunction &#39;or&#39; disjunction
        { $return = [ $item[1], @{$item[2]} ] }
       |    conjunction
        { $return = [ $item[1] ] }

    conjunction:    atom &#39;and&#39; conjunction
        { $return = [ $item[1], @{$item[2]} ] }
       |    atom
        { $return = [ $item[1] ] }</code></pre>

<p>and either way is ugly and hard to get right.</p>

<p>The <code>&lt;leftop:...&gt;</code> and <code>&lt;rightop:...&gt;</code> directives provide an easier way of specifying such operations. Using <code>&lt;leftop:...&gt;</code> the above examples become:</p>

<pre><code>    disjunction:    &lt;leftop: conjunction &#39;or&#39; conjunction&gt;
    conjunction:    &lt;leftop: atom &#39;and&#39; atom&gt;</code></pre>

<p>The <code>&lt;leftop:...&gt;</code> directive specifies a left-associative binary operator. It is specified around three other grammar elements (typically subrules or terminals), which match the left operand, the operator itself, and the right operand respectively.</p>

<p>A <code>&lt;leftop:...&gt;</code> directive such as:</p>

<pre><code>    disjunction:    &lt;leftop: conjunction &#39;or&#39; conjunction&gt;</code></pre>

<p>is converted to the following:</p>

<pre><code>    disjunction:    ( conjunction (&#39;or&#39; conjunction)(s?)
        { $return = [ $item[1], @{$item[2]} ] } )</code></pre>

<p>In other words, a <code>&lt;leftop:...&gt;</code> directive matches the left operand followed by zero or more repetitions of both the operator and the right operand. It then flattens the matched items into an anonymous array which becomes the (single) value of the entire <code>&lt;leftop:...&gt;</code> directive.</p>

<p>For example, an <code>&lt;leftop:...&gt;</code> directive such as:</p>

<pre><code>    output:  &lt;leftop: ident &#39;&lt;&lt;&#39; expr &gt;</code></pre>

<p>when given a string such as:</p>

<pre><code>    cout &lt;&lt; var &lt;&lt; &quot;str&quot; &lt;&lt; 3</code></pre>

<p>would match, and <code>$item[1]</code> would be set to:</p>

<pre><code>    [ &#39;cout&#39;, &#39;var&#39;, &#39;&quot;str&quot;&#39;, &#39;3&#39; ]</code></pre>

<p>In other words:</p>

<pre><code>    output:  &lt;leftop: ident &#39;&lt;&lt;&#39; expr &gt;</code></pre>

<p>is equivalent to a left-associative operator:</p>

<pre><code>    output:  ident          { $return = [$item[1]]   }
          |  ident &#39;&lt;&lt;&#39; expr        { $return = [@item[1,3]]     }
          |  ident &#39;&lt;&lt;&#39; expr &#39;&lt;&lt;&#39; expr      { $return = [@item[1,3,5]]   }
          |  ident &#39;&lt;&lt;&#39; expr &#39;&lt;&lt;&#39; expr &#39;&lt;&lt;&#39; expr    { $return = [@item[1,3,5,7]] }
          #  ...etc...</code></pre>

<p>Similarly, the <code>&lt;rightop:...&gt;</code> directive takes a left operand, an operator, and a right operand:</p>

<pre><code>    assign:  &lt;rightop: var &#39;=&#39; expr &gt;</code></pre>

<p>and converts them to:</p>

<pre><code>    assign:  ( (var &#39;=&#39; {$return=$item[1]})(s?) expr
        { $return = [ @{$item[1]}, $item[2] ] } )</code></pre>

<p>which is equivalent to a right-associative operator:</p>

<pre><code>    assign:  expr       { $return = [$item[1]]       }
          |  var &#39;=&#39; expr       { $return = [@item[1,3]]     }
          |  var &#39;=&#39; var &#39;=&#39; expr   { $return = [@item[1,3,5]]   }
          |  var &#39;=&#39; var &#39;=&#39; var &#39;=&#39; expr   { $return = [@item[1,3,5,7]] }
          #  ...etc...</code></pre>

<p>Note that for both the <code>&lt;leftop:...&gt;</code> and <code>&lt;rightop:...&gt;</code> directives, the directive does not normally return the operator itself, just a list of the operands involved. This is particularly handy for specifying lists:</p>

<pre><code>    list: &#39;(&#39; &lt;leftop: list_item &#39;,&#39; list_item&gt; &#39;)&#39;
        { $return = $item[2] }</code></pre>

<p>There is, however, a problem: sometimes the operator is itself significant. For example, in a Perl list a comma and a <code>=&gt;</code> are both valid separators, but the <code>=&gt;</code> has additional stringification semantics. Hence it&#39;s important to know which was used in each case.</p>

<p>To solve this problem the <code>&lt;leftop:...&gt;</code> and <code>&lt;rightop:...&gt;</code> directives <i>do</i> return the operator(s) as well, under two circumstances. The first case is where the operator is specified as a subrule. In that instance, whatever the operator matches is returned (on the assumption that if the operator is important enough to have its own subrule, then it&#39;s important enough to return).</p>

<p>The second case is where the operator is specified as a regular expression. In that case, if the first bracketed subpattern of the regular expression matches, that matching value is returned (this is analogous to the behaviour of the Perl <code>split</code> function, except that only the first subpattern is returned).</p>

<p>In other words, given the input:</p>

<pre><code>    ( a=&gt;1, b=&gt;2 )</code></pre>

<p>the specifications:</p>

<pre><code>    list:      &#39;(&#39;  &lt;leftop: list_item separator list_item&gt;  &#39;)&#39;

    separator: &#39;,&#39; | &#39;=&gt;&#39;</code></pre>

<p>or:</p>

<pre><code>    list:      &#39;(&#39;  &lt;leftop: list_item /(,|=&gt;)/ list_item&gt;  &#39;)&#39;</code></pre>

<p>cause the list separators to be interleaved with the operands in the anonymous array in <code>$item[2]</code>:</p>

<pre><code>    [ &#39;a&#39;, &#39;=&gt;&#39;, &#39;1&#39;, &#39;,&#39;, &#39;b&#39;, &#39;=&gt;&#39;, &#39;2&#39; ]</code></pre>

<p>But the following version:</p>

<pre><code>    list:      &#39;(&#39;  &lt;leftop: list_item /,|=&gt;/ list_item&gt;  &#39;)&#39;</code></pre>

<p>returns only the operators:</p>

<pre><code>    [ &#39;a&#39;, &#39;1&#39;, &#39;b&#39;, &#39;2&#39; ]</code></pre>

<p>Of course, none of the above specifications handle the case of an empty list, since the <code>&lt;leftop:...&gt;</code> and <code>&lt;rightop:...&gt;</code> directives require at least a single right or left operand to match. To specify that the operator can match &quot;trivially&quot;, it&#39;s necessary to add a <code>(s?)</code> qualifier to the directive:</p>

<pre><code>    list:      &#39;(&#39;  &lt;leftop: list_item /(,|=&gt;)/ list_item&gt;(s?)  &#39;)&#39;</code></pre>

<p>Note that in almost all the above examples, the first and third arguments of the <code>&lt;leftop:...&gt;</code> directive were the same subrule. That is because <code>&lt;leftop:...&gt;</code>&#39;s are frequently used to specify &quot;separated&quot; lists of the same type of item. To make such lists easier to specify, the following syntax:</p>

<pre><code>    list:   element(s /,/)</code></pre>

<p>is exactly equivalent to:</p>

<pre><code>    list:   &lt;leftop: element /,/ element&gt;</code></pre>

<p>Note that the separator must be specified as a raw pattern (i.e. not a string or subrule).</p>

</dd>
<dt id="Scored-productions"><a id="Scored"></a>Scored productions</dt>
<dd>

<p>By default, Parse::RecDescent grammar rules always accept the first production that matches the input. But if two or more productions may potentially match the same input, choosing the first that does so may not be optimal.</p>

<p>For example, if you were parsing the sentence &quot;time flies like an arrow&quot;, you might use a rule like this:</p>

<pre><code>    sentence: verb noun preposition article noun { [@item] }
    | adjective noun verb article noun   { [@item] }
    | noun verb preposition article noun { [@item] }</code></pre>

<p>Each of these productions matches the sentence, but the third one is the most likely interpretation. However, if the sentence had been &quot;fruit flies like a banana&quot;, then the second production is probably the right match.</p>

<p>To cater for such situations, the <code>&lt;score:...&gt;</code> can be used. The directive is equivalent to an unconditional <code>&lt;reject&gt;</code>, except that it allows you to specify a &quot;score&quot; for the current production. If that score is numerically greater than the best score of any preceding production, the current production is cached for later consideration. If no later production matches, then the cached production is treated as having matched, and the value of the item immediately before its <code>&lt;score:...&gt;</code> directive is returned as the result.</p>

<p>In other words, by putting a <code>&lt;score:...&gt;</code> directive at the end of each production, you can select which production matches using criteria other than specification order. For example:</p>

<pre><code>    sentence: verb noun preposition article noun { [@item] } &lt;score: sensible(@item)&gt;
    | adjective noun verb article noun   { [@item] } &lt;score: sensible(@item)&gt;
    | noun verb preposition article noun { [@item] } &lt;score: sensible(@item)&gt;</code></pre>

<p>Now, when each production reaches its respective <code>&lt;score:...&gt;</code> directive, the subroutine <code>sensible</code> will be called to evaluate the matched items (somehow). Once all productions have been tried, the one which <code>sensible</code> scored most highly will be the one that is accepted as a match for the rule.</p>

<p>The variable $score always holds the current best score of any production, and the variable $score_return holds the corresponding return value.</p>

<p>As another example, the following grammar matches lines that may be separated by commas, colons, or semi-colons. This can be tricky if a colon-separated line also contains commas, or vice versa. The grammar resolves the ambiguity by selecting the rule that results in the fewest fields:</p>

<pre><code>    line: seplist[sep=&gt;&#39;,&#39;]  &lt;score: -@{$item[1]}&gt;
    | seplist[sep=&gt;&#39;:&#39;]  &lt;score: -@{$item[1]}&gt;
    | seplist[sep=&gt;&quot; &quot;]  &lt;score: -@{$item[1]}&gt;

    seplist: &lt;skip:&quot;&quot;&gt; &lt;leftop: /[^$arg{sep}]*/ &quot;$arg{sep}&quot; /[^$arg{sep}]*/&gt;</code></pre>

<p>Note the use of negation within the <code>&lt;score:...&gt;</code> directive to ensure that the seplist with the most items gets the lowest score.</p>

<p>As the above examples indicate, it is often the case that all productions in a rule use exactly the same <code>&lt;score:...&gt;</code> directive. It is tedious to have to repeat this identical directive in every production, so Parse::RecDescent also provides the <code>&lt;autoscore:...&gt;</code> directive.</p>

<p>If an <code>&lt;autoscore:...&gt;</code> directive appears in any production of a rule, the code it specifies is used as the scoring code for every production of that rule, except productions that already end with an explicit <code>&lt;score:...&gt;</code> directive. Thus the rules above could be rewritten:</p>

<pre><code>    line: &lt;autoscore: -@{$item[1]}&gt;
    line: seplist[sep=&gt;&#39;,&#39;]
    | seplist[sep=&gt;&#39;:&#39;]
    | seplist[sep=&gt;&quot; &quot;]


    sentence: &lt;autoscore: sensible(@item)&gt;
    | verb noun preposition article noun { [@item] }
    | adjective noun verb article noun   { [@item] }
    | noun verb preposition article noun { [@item] }</code></pre>

<p>Note that the <code>&lt;autoscore:...&gt;</code> directive itself acts as an unconditional <code>&lt;reject&gt;</code>, and (like the <code>&lt;rulevar:...&gt;</code> directive) is pruned at compile-time wherever possible.</p>

</dd>
<dt id="Dispensing-with-grammar-checks"><a id="Dispensing"></a>Dispensing with grammar checks</dt>
<dd>

<p>During the compilation phase of parser construction, Parse::RecDescent performs a small number of checks on the grammar it&#39;s given. Specifically it checks that the grammar is not left-recursive, that there are no &quot;insatiable&quot; constructs of the form:</p>

<pre><code>    rule: subrule(s) subrule</code></pre>

<p>and that there are no rules missing (i.e. referred to, but never defined).</p>

<p>These checks are important during development, but can slow down parser construction in stable code. So Parse::RecDescent provides the &lt;nocheck&gt; directive to turn them off. The directive can only appear before the first rule definition, and switches off checking throughout the rest of the current grammar.</p>

<p>Typically, this directive would be added when a parser has been thoroughly tested and is ready for release.</p>

</dd>
</dl>

<h2 id="Subrule-argument-lists"><a id="Subrule"></a>Subrule argument lists</h2>

<p>It is occasionally useful to pass data to a subrule which is being invoked. For example, consider the following grammar fragment:</p>

<pre><code>    classdecl: keyword decl

    keyword:   &#39;struct&#39; | &#39;class&#39;;

    decl:      # WHATEVER</code></pre>

<p>The <code>decl</code> rule might wish to know which of the two keywords was used (since it may affect some aspect of the way the subsequent declaration is interpreted). <code>Parse::RecDescent</code> allows the grammar designer to pass data into a rule, by placing that data in an <i>argument list</i> (that is, in square brackets) immediately after any subrule item in a production. Hence, we could pass the keyword to <code>decl</code> as follows:</p>

<pre><code>    classdecl: keyword decl[ $item[1] ]

    keyword:   &#39;struct&#39; | &#39;class&#39;;

    decl:      # WHATEVER</code></pre>

<p>The argument list can consist of any number (including zero!) of comma-separated Perl expressions. In other words, it looks exactly like a Perl anonymous array reference. For example, we could pass the keyword, the name of the surrounding rule, and the literal &#39;keyword&#39; to <code>decl</code> like so:</p>

<pre><code>    classdecl: keyword decl[$item[1],$item[0],&#39;keyword&#39;]

    keyword:   &#39;struct&#39; | &#39;class&#39;;

    decl:      # WHATEVER</code></pre>

<p>Within the rule to which the data is passed (<code>decl</code> in the above examples) that data is available as the elements of a local variable <code>@arg</code>. Hence <code>decl</code> might report its intentions as follows:</p>

<pre><code>    classdecl: keyword decl[$item[1],$item[0],&#39;keyword&#39;]

    keyword:   &#39;struct&#39; | &#39;class&#39;;

    decl:      { print &quot;Declaring $arg[0] (a $arg[2])\n&quot;;
         print &quot;(this rule called by $arg[1])&quot; }</code></pre>

<p>Subrule argument lists can also be interpreted as hashes, simply by using the local variable <code>%arg</code> instead of <code>@arg</code>. Hence we could rewrite the previous example:</p>

<pre><code>    classdecl: keyword decl[keyword =&gt; $item[1],
        caller  =&gt; $item[0],
        type    =&gt; &#39;keyword&#39;]

    keyword:   &#39;struct&#39; | &#39;class&#39;;

    decl:      { print &quot;Declaring $arg{keyword} (a $arg{type})\n&quot;;
         print &quot;(this rule called by $arg{caller})&quot; }</code></pre>

<p>Both <code>@arg</code> and <code>%arg</code> are always available, so the grammar designer may choose whichever convention (or combination of conventions) suits best.</p>

<p>Subrule argument lists are also useful for creating &quot;rule templates&quot; (especially when used in conjunction with the <code>&lt;matchrule:...&gt;</code> directive). For example, the subrule:</p>

<pre><code>    list:     &lt;matchrule:$arg{rule}&gt; /$arg{sep}/ list[%arg]
        { $return = [ $item[1], @{$item[3]} ] }
    |     &lt;matchrule:$arg{rule}&gt;
        { $return = [ $item[1]] }</code></pre>

<p>is a handy template for the common problem of matching a separated list. For example:</p>

<pre><code>    function: &#39;func&#39; name &#39;(&#39; list[rule=&gt;&#39;param&#39;,sep=&gt;&#39;;&#39;] &#39;)&#39;

    param:    list[rule=&gt;&#39;name&#39;,sep=&gt;&#39;,&#39;] &#39;:&#39; typename

    name:     /\w+/

    typename: name</code></pre>

<p>When a subrule argument list is used with a repeated subrule, the argument list goes <i>before</i> the repetition specifier:</p>

<pre><code>    list:   /some|many/ thing[ $item[1] ](s)</code></pre>

<p>The argument list is &quot;late bound&quot;. That is, it is re-evaluated for every repetition of the repeated subrule. This means that each repeated attempt to match the subrule may be passed a completely different set of arguments if the value of the expression in the argument list changes between attempts. So, for example, the grammar:</p>

<pre><code>    { $::species = &#39;dogs&#39; }

    pair:   &#39;two&#39; animal[$::species](s)

    animal: /$arg[0]/ { $::species = &#39;cats&#39; }</code></pre>

<p>will match the string &quot;two dogs cats cats&quot; completely, whereas it will only match the string &quot;two dogs dogs dogs&quot; up to the eighth letter. If the value of the argument list were &quot;early bound&quot; (that is, evaluated only the first time a repeated subrule match is attempted), one would expect the matching behaviours to be reversed.</p>

<p>Of course, it is possible to effectively &quot;early bind&quot; such argument lists by passing them a value which does not change on each repetition. For example:</p>

<pre><code>    { $::species = &#39;dogs&#39; }

    pair:   &#39;two&#39; { $::species } animal[$item[2]](s)

    animal: /$arg[0]/ { $::species = &#39;cats&#39; }</code></pre>

<p>Arguments can also be passed to the start rule, simply by appending them to the argument list with which the start rule is called (<i>after</i> the &quot;line number&quot; parameter). For example, given:</p>

<pre><code>    $parser = new Parse::RecDescent ( $grammar );

    $parser-&gt;data($text, 1, &quot;str&quot;, 2, \@arr);

    #         ^^^^^  ^  ^^^^^^^^^^^^^^^
    #       |    |     |
    # TEXT TO BE PARSED  |     |
    # STARTING LINE NUMBER     |
    # ELEMENTS OF @arg WHICH IS PASSED TO RULE data</code></pre>

<p>then within the productions of the rule <code>data</code>, the array <code>@arg</code> will contain <code>(&quot;str&quot;, 2, \@arr)</code>.</p>

<h2 id="Alternations">Alternations</h2>

<p>Alternations are implicit (unnamed) rules defined as part of a production. An alternation is defined as a series of &#39;|&#39;-separated productions inside a pair of round brackets. For example:</p>

<pre><code>    character: &#39;the&#39; ( good | bad | ugly ) /dude/</code></pre>

<p>Every alternation implicitly defines a new subrule, whose automatically-generated name indicates its origin: &quot;_alternation_&lt;I&gt;_of_production_&lt;P&gt;_of_rule&lt;R&gt;&quot; for the appropriate values of &lt;I&gt;, &lt;P&gt;, and &lt;R&gt;. A call to this implicit subrule is then inserted in place of the brackets. Hence the above example is merely a convenient short-hand for:</p>

<pre><code>    character: &#39;the&#39;
       _alternation_1_of_production_1_of_rule_character
       /dude/

    _alternation_1_of_production_1_of_rule_character:
       good | bad | ugly</code></pre>

<p>Since alternations are parsed by recursively calling the parser generator, any type(s) of item can appear in an alternation. For example:</p>

<pre><code>    character: &#39;the&#39; ( &#39;high&#39; &quot;plains&quot;  # Silent, with poncho
         | /no[- ]name/ # Silent, no poncho
         | vengeance_seeking    # Poncho-optional
         | &lt;error&gt;
         ) drifter</code></pre>

<p>In this case, if an error occurred, the automatically generated message would be:</p>

<pre><code>    ERROR (line &lt;N&gt;): Invalid implicit subrule: Expected
          &#39;high&#39; or /no[- ]name/ or generic,
          but found &quot;pacifist&quot; instead</code></pre>

<p>Since every alternation actually has a name, it&#39;s even possible to extend or replace them:</p>

<pre><code>    parser-&gt;Replace(
    &quot;_alternation_1_of_production_1_of_rule_character:
        &#39;generic Eastwood&#39;&quot;
        );</code></pre>

<p>More importantly, since alternations are a form of subrule, they can be given repetition specifiers:</p>

<pre><code>    character: &#39;the&#39; ( good | bad | ugly )(?) /dude/</code></pre>

<h2 id="Incremental-Parsing"><a id="Incremental"></a>Incremental Parsing</h2>

<p><code>Parse::RecDescent</code> provides two methods - <code>Extend</code> and <code>Replace</code> - which can be used to alter the grammar matched by a parser. Both methods take the same argument as <code>Parse::RecDescent::new</code>, namely a grammar specification string</p>

<p><code>Parse::RecDescent::Extend</code> interprets the grammar specification and adds any productions it finds to the end of the rules for which they are specified. For example:</p>

<pre><code>    $add = &quot;name: &#39;Jimmy-Bob&#39; | &#39;Bobby-Jim&#39;\ndesc: colour /necks?/&quot;;
    parser-&gt;Extend($add);</code></pre>

<p>adds two productions to the rule &quot;name&quot; (creating it if necessary) and one production to the rule &quot;desc&quot;.</p>

<p><code>Parse::RecDescent::Replace</code> is identical, except that it first resets are rule specified in the additional grammar, removing any existing productions. Hence after:</p>

<pre><code>    $add = &quot;name: &#39;Jimmy-Bob&#39; | &#39;Bobby-Jim&#39;\ndesc: colour /necks?/&quot;;
    parser-&gt;Replace($add);</code></pre>

<p>there are <i>only</i> valid &quot;name&quot;s and the one possible description.</p>

<p>A more interesting use of the <code>Extend</code> and <code>Replace</code> methods is to call them inside the action of an executing parser. For example:</p>

<pre><code>    typedef: &#39;typedef&#39; type_name identifier &#39;;&#39;
           { $thisparser-&gt;Extend(&quot;type_name: &#39;$item[3]&#39;&quot;) }
       | &lt;error&gt;

    identifier: ...!type_name /[A-Za-z_]w*/</code></pre>

<p>which automatically prevents type names from being typedef&#39;d, or:</p>

<pre><code>    command: &#39;map&#39; key_name &#39;to&#39; abort_key
           { $thisparser-&gt;Replace(&quot;abort_key: &#39;$item[2]&#39;&quot;) }
       | &#39;map&#39; key_name &#39;to&#39; key_name
           { map_key($item[2],$item[4]) }
       | abort_key
           { exit if confirm(&quot;abort?&quot;) }

    abort_key: &#39;q&#39;

    key_name: ...!abort_key /[A-Za-z]/</code></pre>

<p>which allows the user to change the abort key binding, but not to unbind it.</p>

<p>The careful use of such constructs makes it possible to reconfigure a a running parser, eliminating the need for semantic feedback by providing syntactic feedback instead. However, as currently implemented, <code>Replace()</code> and <code>Extend()</code> have to regenerate and re-<code>eval</code> the entire parser whenever they are called. This makes them quite slow for large grammars.</p>

<p>In such cases, the judicious use of an interpolated regex is likely to be far more efficient:</p>

<pre><code>    typedef: &#39;typedef&#39; type_name/ identifier &#39;;&#39;
           { $thisparser-&gt;{local}{type_name} .= &quot;|$item[3]&quot; }
       | &lt;error&gt;

    identifier: ...!type_name /[A-Za-z_]w*/

    type_name: /$thisparser-&gt;{local}{type_name}/</code></pre>

<h2 id="Precompiling-parsers"><a id="Precompiling"></a>Precompiling parsers</h2>

<p>Normally Parse::RecDescent builds a parser from a grammar at run-time. That approach simplifies the design and implementation of parsing code, but has the disadvantage that it slows the parsing process down - you have to wait for Parse::RecDescent to build the parser every time the program runs. Long or complex grammars can be particularly slow to build, leading to unacceptable delays at start-up.</p>

<p>To overcome this, the module provides a way of &quot;pre-building&quot; a parser object and saving it in a separate module. That module can then be used to create clones of the original parser.</p>

<p>A grammar may be precompiled using the <code>Precompile</code> class method. For example, to precompile a grammar stored in the scalar $grammar, and produce a class named PreGrammar in a module file named PreGrammar.pm, you could use:</p>

<pre><code>    use Parse::RecDescent;

    Parse::RecDescent-&gt;Precompile([$options_hashref], $grammar, &quot;PreGrammar&quot;, [&quot;RuntimeClass&quot;]);</code></pre>

<p>The first required argument is the grammar string, the second is the name of the class to be built. The name of the module file is generated automatically by appending &quot;.pm&quot; to the last element of the class name. Thus</p>

<pre><code>    Parse::RecDescent-&gt;Precompile($grammar, &quot;My::New::Parser&quot;);</code></pre>

<p>would produce a module file named Parser.pm.</p>

<p>After the class name, you may specify the name of the runtime_class called by the Precompiled parser. See <a href="./Parse::RecDescent.html#Precompiled-runtimes">&quot;Precompiled runtimes&quot;</a> for more details.</p>

<p>An optional hash reference may be supplied as the first argument to <code>Precompile</code>. This argument is currently EXPERIMENTAL, and may change in a future release of Parse::RecDescent. The only supported option is currently <code>-standalone</code>, see <a href="./Parse::RecDescent.html#Standalone-precompiled-parsers">&quot;Standalone precompiled parsers&quot;</a>.</p>

<p>It is somewhat tedious to have to write a small Perl program just to generate a precompiled grammar class, so Parse::RecDescent has some special magic that allows you to do the job directly from the command-line.</p>

<p>If your grammar is specified in a file named <i>grammar</i>, you can generate a class named Yet::Another::Grammar like so:</p>

<pre><code>    &gt; perl -MParse::RecDescent - grammar Yet::Another::Grammar [Runtime::Class]</code></pre>

<p>This would produce a file named <i>Grammar.pm</i> containing the full definition of a class called Yet::Another::Grammar. Of course, to use that class, you would need to put the <i>Grammar.pm</i> file in a directory named <i>Yet/Another</i>, somewhere in your Perl include path.</p>

<p>Having created the new class, it&#39;s very easy to use it to build a parser. You simply <code>use</code> the new module, and then call its <code>new</code> method to create a parser object. For example:</p>

<pre><code>    use Yet::Another::Grammar;
    my $parser = Yet::Another::Grammar-&gt;new();</code></pre>

<p>The effect of these two lines is exactly the same as:</p>

<pre><code>    use Parse::RecDescent;

    open GRAMMAR_FILE, &quot;grammar&quot; or die;
    local $/;
    my $grammar = &lt;GRAMMAR_FILE&gt;;

    my $parser = Parse::RecDescent-&gt;new($grammar);</code></pre>

<p>only considerably faster.</p>

<p>Note however that the parsers produced by either approach are exactly the same, so whilst precompilation has an effect on <i>set-up</i> speed, it has no effect on <i>parsing</i> speed. RecDescent 2.0 will address that problem.</p>

<h3 id="Standalone-precompiled-parsers"><a id="Standalone"></a>Standalone precompiled parsers</h3>

<p>Until version 1.967003 of Parse::RecDescent, parser modules built with <code>Precompile</code> were dependent on Parse::RecDescent. Future Parse::RecDescent releases with different internal implementations would break pre-existing precompiled parsers.</p>

<p>Version 1.967_005 added the ability for Parse::RecDescent to include itself in the resulting .pm file if you pass the boolean option <code>-standalone</code> to <code>Precompile</code>:</p>

<pre><code>    Parse::RecDescent-&gt;Precompile({ -standalone =&gt; 1, },
        $grammar, &quot;My::New::Parser&quot;);</code></pre>

<p>Parse::RecDescent is included as <code>$class::_Runtime</code> in order to avoid conflicts between an installed version of Parse::RecDescent and other precompiled, standalone parser made with Parse::RecDescent. The name of this class may be changed with the <code>-runtime_class</code> option to Precompile. This renaming is experimental, and is subject to change in future versions.</p>

<p>Precompiled parsers remain dependent on Parse::RecDescent by default, as this feature is still considered experimental. In the future, standalone parsers will become the default.</p>

<h3 id="Precompiled-runtimes"><a id="Precompiled"></a>Precompiled runtimes</h3>

<p>Standalone precompiled parsers each include a copy of Parse::RecDescent. For users who have a family of related precompiled parsers, this is very inefficient. <code>Precompile</code> now supports an experimental <code>-runtime_class</code> option. To build a precompiled parser with a different runtime name, call:</p>

<pre><code>    Parse::RecDescent-&gt;Precompile({
            -standalone =&gt; 1,
            -runtime_class =&gt; &quot;My::Runtime&quot;,
        },
        $grammar, &quot;My::New::Parser&quot;);</code></pre>

<p>The resulting standalone parser will contain a copy of Parse::RecDescent, renamed to &quot;My::Runtime&quot;.</p>

<p>To build a set of parsers that <code>use</code> a custom-named runtime, without including that runtime in the output, simply build those parsers with <code>-runtime_class</code> and without <code>-standalone</code>:</p>

<pre><code>    Parse::RecDescent-&gt;Precompile({
            -runtime_class =&gt; &quot;My::Runtime&quot;,
        },
        $grammar, &quot;My::New::Parser&quot;);</code></pre>

<p>The runtime itself must be generated as well, so that it may be <code>use</code>d by My::New::Parser. To generate the runtime file, use one of the two folling calls:</p>

<pre><code>    Parse::RecDescent-&gt;PrecompiledRuntime(&quot;My::Runtime&quot;);

    Parse::RecDescent-&gt;Precompile({
            -standalone =&gt; 1,
            -runtime_class =&gt; &quot;My::Runtime&quot;,
        },
        &#39;&#39;, # empty grammar
        &quot;My::Runtime&quot;);</code></pre>

<h1 id="GOTCHAS">GOTCHAS</h1>

<p>This section describes common mistakes that grammar writers seem to make on a regular basis.</p>

<h2 id="1.-Expecting-an-error-to-always-invalidate-a-parse"><a id="1"></a><a id="Expecting-an-error-to-always-invalidate-a-parse"></a>1. Expecting an error to always invalidate a parse</h2>

<p>A common mistake when using error messages is to write the grammar like this:</p>

<pre><code>    file: line(s)

    line: line_type_1
    | line_type_2
    | line_type_3
    | &lt;error&gt;</code></pre>

<p>The expectation seems to be that any line that is not of type 1, 2 or 3 will invoke the <code>&lt;error&gt;</code> directive and thereby cause the parse to fail.</p>

<p>Unfortunately, that only happens if the error occurs in the very first line. The first rule states that a <code>file</code> is matched by one or more lines, so if even a single line succeeds, the first rule is completely satisfied and the parse as a whole succeeds. That means that any error messages generated by subsequent failures in the <code>line</code> rule are quietly ignored.</p>

<p>Typically what&#39;s really needed is this:</p>

<pre><code>    file: line(s) eofile    { $return = $item[1] }

    line: line_type_1
    | line_type_2
    | line_type_3
    | &lt;error&gt;

    eofile: /^\Z/</code></pre>

<p>The addition of the <code>eofile</code> subrule to the first production means that a file only matches a series of successful <code>line</code> matches <i>that consume the complete input text</i>. If any input text remains after the lines are matched, there must have been an error in the last <code>line</code>. In that case the <code>eofile</code> rule will fail, causing the entire <code>file</code> rule to fail too.</p>

<p>Note too that <code>eofile</code> must match <code>/^\Z/</code> (end-of-text), <i>not</i> <code>/^\cZ/</code> or <code>/^\cD/</code> (end-of-file).</p>

<p>And don&#39;t forget the action at the end of the production. If you just write:</p>

<pre><code>    file: line(s) eofile</code></pre>

<p>then the value returned by the <code>file</code> rule will be the value of its last item: <code>eofile</code>. Since <code>eofile</code> always returns an empty string on success, that will cause the <code>file</code> rule to return that empty string. Apart from returning the wrong value, returning an empty string will trip up code such as:</p>

<pre><code>    $parser-&gt;file($filetext) || die;</code></pre>

<p>(since &quot;&quot; is false).</p>

<p>Remember that Parse::RecDescent returns undef on failure, so the only safe test for failure is:</p>

<pre><code>    defined($parser-&gt;file($filetext)) || die;</code></pre>

<h2 id="2.-Using-a-return-in-an-action"><a id="2"></a><a id="Using-a-return-in-an-action"></a>2. Using a <code>return</code> in an action</h2>

<p>An action is like a <code>do</code> block inside the subroutine implementing the surrounding rule. So if you put a <code>return</code> statement in an action:</p>

<pre><code>    range: &#39;(&#39; start &#39;..&#39; end )&#39;
        { return $item{end} }
       /\s+/</code></pre>

<p>that subroutine will immediately return, without checking the rest of the items in the current production (e.g. the <code>/\s+/</code>) and without setting up the necessary data structures to tell the parser that the rule has succeeded.</p>

<p>The correct way to set a return value in an action is to set the <code>$return</code> variable:</p>

<pre><code>    range: &#39;(&#39; start &#39;..&#39; end )&#39;
                { $return = $item{end} }
           /\s+/</code></pre>

<h2 id="2.-Setting-$Parse::RecDescent::skip-at-parse-time"><a id="21"></a><a id="Setting--Parse::RecDescent::skip-at-parse-time"></a>2. Setting <code>$Parse::RecDescent::skip</code> at parse time</h2>

<p>If you want to change the default skipping behaviour (see <a href="./Parse::RecDescent.html#Terminal-Separators">&quot;Terminal Separators&quot;</a> and the <code>&lt;skip:...&gt;</code> directive) by setting <code>$Parse::RecDescent::skip</code> you have to remember to set this variable <i>before</i> creating the grammar object.</p>

<p>For example, you might want to skip all Perl-like comments with this regular expression:</p>

<pre><code>   my $skip_spaces_and_comments = qr/
         (?mxs:
            \s+         # either spaces
            | \# .*?$   # or a dash and whatever up to the end of line
         )*             # repeated at will (in whatever order)
      /;</code></pre>

<p>And then:</p>

<pre><code>   my $parser1 = Parse::RecDescent-&gt;new($grammar);

   $Parse::RecDescent::skip = $skip_spaces_and_comments;

   my $parser2 = Parse::RecDescent-&gt;new($grammar);

   $parser1-&gt;parse($text); # this does not cope with comments
   $parser2-&gt;parse($text); # this skips comments correctly</code></pre>

<p>The two parsers behave differently, because any skipping behaviour specified via <code>$Parse::RecDescent::skip</code> is hard-coded when the grammar object is built, not at parse time.</p>

<h1 id="DIAGNOSTICS">DIAGNOSTICS</h1>

<p>Diagnostics are intended to be self-explanatory (particularly if you use <b>-RD_HINT</b> (under <b>perl -s</b>) or define <code>$::RD_HINT</code> inside the program).</p>

<p><code>Parse::RecDescent</code> currently diagnoses the following:</p>

<ul>

<li><p>Invalid regular expressions used as pattern terminals (fatal error).</p>

</li>
<li><p>Invalid Perl code in code blocks (fatal error).</p>

</li>
<li><p>Lookahead used in the wrong place or in a nonsensical way (fatal error).</p>

</li>
<li><p>&quot;Obvious&quot; cases of left-recursion (fatal error).</p>

</li>
<li><p>Missing or extra components in a <code>&lt;leftop&gt;</code> or <code>&lt;rightop&gt;</code> directive.</p>

</li>
<li><p>Unrecognisable components in the grammar specification (fatal error).</p>

</li>
<li><p>&quot;Orphaned&quot; rule components specified before the first rule (fatal error) or after an <code>&lt;error&gt;</code> directive (level 3 warning).</p>

</li>
<li><p>Missing rule definitions (this only generates a level 3 warning, since you may be providing them later via <code>Parse::RecDescent::Extend()</code>).</p>

</li>
<li><p>Instances where greedy repetition behaviour will almost certainly cause the failure of a production (a level 3 warning - see <a href="./Parse::RecDescent.html#ON-GOING-ISSUES-AND-FUTURE-DIRECTIONS">&quot;ON-GOING ISSUES AND FUTURE DIRECTIONS&quot;</a> below).</p>

</li>
<li><p>Attempts to define rules named &#39;Replace&#39; or &#39;Extend&#39;, which cannot be called directly through the parser object because of the predefined meaning of <code>Parse::RecDescent::Replace</code> and <code>Parse::RecDescent::Extend</code>. (Only a level 2 warning is generated, since such rules <i>can</i> still be used as subrules).</p>

</li>
<li><p>Productions which consist of a single <code>&lt;error?&gt;</code> directive, and which therefore may succeed unexpectedly (a level 2 warning, since this might conceivably be the desired effect).</p>

</li>
<li><p>Multiple consecutive lookahead specifiers (a level 1 warning only, since their effects simply accumulate).</p>

</li>
<li><p>Productions which start with a <code>&lt;reject&gt;</code> or <code>&lt;rulevar:...&gt;</code> directive. Such productions are optimized away (a level 1 warning).</p>

</li>
<li><p>Rules which are autogenerated under <code>$::AUTOSTUB</code> (a level 1 warning).</p>

</li>
</ul>

<h1 id="AUTHOR">AUTHOR</h1>

<p>Damian Conway (damian@conway.org) Jeremy T. Braun (JTBRAUN@CPAN.org) [current maintainer]</p>

<h1 id="BUGS-AND-IRRITATIONS"><a id="BUGS"></a>BUGS AND IRRITATIONS</h1>

<p>There are undoubtedly serious bugs lurking somewhere in this much code :-) Bug reports, test cases and other feedback are most welcome.</p>

<p>Ongoing annoyances include:</p>

<ul>

<li><p>There&#39;s no support for parsing directly from an input stream. If and when the Perl Gods give us regular expressions on streams, this should be trivial (ahem!) to implement.</p>

</li>
<li><p>The parser generator can get confused if actions aren&#39;t properly closed or if they contain particularly nasty Perl syntax errors (especially unmatched curly brackets).</p>

</li>
<li><p>The generator only detects the most obvious form of left recursion (potential recursion on the first subrule in a rule). More subtle forms of left recursion (for example, through the second item in a rule after a &quot;zero&quot; match of a preceding &quot;zero-or-more&quot; repetition, or after a match of a subrule with an empty production) are not found.</p>

</li>
<li><p>Instead of complaining about left-recursion, the generator should silently transform the grammar to remove it. Don&#39;t expect this feature any time soon as it would require a more sophisticated approach to parser generation than is currently used.</p>

</li>
<li><p>The generated parsers don&#39;t always run as fast as might be wished.</p>

</li>
<li><p>The meta-parser should be bootstrapped using <code>Parse::RecDescent</code> :-)</p>

</li>
</ul>

<h1 id="ON-GOING-ISSUES-AND-FUTURE-DIRECTIONS"><a id="ON"></a>ON-GOING ISSUES AND FUTURE DIRECTIONS</h1>

<ol>

<li><p>Repetitions are &quot;incorrigibly greedy&quot; in that they will eat everything they can and won&#39;t backtrack if that behaviour causes a production to fail needlessly. So, for example:</p>

<pre><code>    rule: subrule(s) subrule</code></pre>

<p>will <i>never</i> succeed, because the repetition will eat all the subrules it finds, leaving none to match the second item. Such constructions are relatively rare (and <code>Parse::RecDescent::new</code> generates a warning whenever they occur) so this may not be a problem, especially since the insatiable behaviour can be overcome &quot;manually&quot; by writing:</p>

<pre><code>    rule: penultimate_subrule(s) subrule

    penultimate_subrule: subrule ...subrule</code></pre>

<p>The issue is that this construction is exactly twice as expensive as the original, whereas backtracking would add only 1/<i>N</i> to the cost (for matching <i>N</i> repetitions of <code>subrule</code>). I would welcome feedback on the need for backtracking; particularly on cases where the lack of it makes parsing performance problematical.</p>

</li>
<li><p>Having opened that can of worms, it&#39;s also necessary to consider whether there is a need for non-greedy repetition specifiers. Again, it&#39;s possible (at some cost) to manually provide the required functionality:</p>

<pre><code>    rule: nongreedy_subrule(s) othersubrule

    nongreedy_subrule: subrule ...!othersubrule</code></pre>

<p>Overall, the issue is whether the benefit of this extra functionality outweighs the drawbacks of further complicating the (currently minimalist) grammar specification syntax, and (worse) introducing more overhead into the generated parsers.</p>

</li>
<li><p>An <code>&lt;autocommit&gt;</code> directive would be nice. That is, it would be useful to be able to say:</p>

<pre><code>    command: &lt;autocommit&gt;
    command: &#39;find&#39; name
       | &#39;find&#39; address
       | &#39;do&#39; command &#39;at&#39; time &#39;if&#39; condition
       | &#39;do&#39; command &#39;at&#39; time
       | &#39;do&#39; command
       | unusual_command</code></pre>

<p>and have the generator work out that this should be &quot;pruned&quot; thus:</p>

<pre><code>    command: &#39;find&#39; name
       | &#39;find&#39; &lt;commit&gt; address
       | &#39;do&#39; &lt;commit&gt; command &lt;uncommit&gt;
        &#39;at&#39; time
        &#39;if&#39; &lt;commit&gt; condition
       | &#39;do&#39; &lt;commit&gt; command &lt;uncommit&gt;
        &#39;at&#39; &lt;commit&gt; time
       | &#39;do&#39; &lt;commit&gt; command
       | unusual_command</code></pre>

<p>There are several issues here. Firstly, should the <code>&lt;autocommit&gt;</code> automatically install an <code>&lt;uncommit&gt;</code> at the start of the last production (on the grounds that the &quot;command&quot; rule doesn&#39;t know whether an &quot;unusual_command&quot; might start with &quot;find&quot; or &quot;do&quot;) or should the &quot;unusual_command&quot; subgraph be analysed (to see if it <i>might</i> be viable after a &quot;find&quot; or &quot;do&quot;)?</p>

<p>The second issue is how regular expressions should be treated. The simplest approach would be simply to uncommit before them (on the grounds that they <i>might</i> match). Better efficiency would be obtained by analyzing all preceding literal tokens to determine whether the pattern would match them.</p>

<p>Overall, the issues are: can such automated &quot;pruning&quot; approach a hand-tuned version sufficiently closely to warrant the extra set-up expense, and (more importantly) is the problem important enough to even warrant the non-trivial effort of building an automated solution?</p>

</li>
</ol>

<h1 id="SUPPORT">SUPPORT</h1>

<h2 id="Source-Code-Repository"><a id="Source"></a>Source Code Repository</h2>

<p><a href="http://github.com/jtbraun/Parse-RecDescent">http://github.com/jtbraun/Parse-RecDescent</a></p>

<h2 id="Mailing-List"><a id="Mailing"></a>Mailing List</h2>

<p>Visit <a href="http://www.perlfoundation.org/perl5/index.cgi?parse_recdescent">http://www.perlfoundation.org/perl5/index.cgi?parse_recdescent</a> to sign up for the mailing list.</p>

<p><a href="http://www.PerlMonks.org">http://www.PerlMonks.org</a> is also a good place to ask questions. Previous posts about Parse::RecDescent can typically be found with this search: <a href="http://perlmonks.org/index.pl?node=recdescent">http://perlmonks.org/index.pl?node=recdescent</a>.</p>

<h2 id="FAQ">FAQ</h2>

<p>Visit <a href="https://metacpan.org/pod/Parse::RecDescent::FAQ">Parse::RecDescent::FAQ</a> for answers to frequently (and not so frequently) asked questions about Parse::RecDescent.</p>

<h2 id="View/Report-Bugs"><a id="View"></a><a id="View-Report-Bugs"></a>View/Report Bugs</h2>

<p>To view the current bug list or report a new issue visit <a href="https://rt.cpan.org/Public/Dist/Display.html?Name=Parse-RecDescent">https://rt.cpan.org/Public/Dist/Display.html?Name=Parse-RecDescent</a>.</p>

<h1 id="SEE-ALSO"><a id="SEE"></a>SEE ALSO</h1>

<p><a href="./Regexp::Grammars.html">Regexp::Grammars</a> provides Parse::RecDescent style parsing using native Perl 5.10 regular expressions.</p>

<h1 id="LICENCE-AND-COPYRIGHT"><a id="LICENCE"></a>LICENCE AND COPYRIGHT</h1>

<p>Copyright (c) 1997-2007, Damian Conway <code>&lt;DCONWAY@CPAN.org&gt;</code>. All rights reserved.</p>

<p>This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <a href="https://metacpan.org/pod/perlartistic">perlartistic</a>.</p>

<h1 id="DISCLAIMER-OF-WARRANTY"><a id="DISCLAIMER"></a>DISCLAIMER OF WARRANTY</h1>

<p>BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.</p>

<p>IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.</p></div>

<div id="metacpan_install-instructions-dialog" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Module Install Instructions</h4>
      </div>
      <div class="modal-body">
        <p>To install Parse::RecDescent, copy and paste the appropriate command in to your terminal.</p>
        <p><a href="https://metacpan.org/dist/App-cpanminus/view/bin/cpanm">cpanm</a></p>
        <pre><code>cpanm Parse::RecDescent</code></pre>
        <p><a href="https://metacpan.org/pod/CPAN">CPAN shell</a></p>
        <pre><code>perl -MCPAN -e shell
install Parse::RecDescent</code></pre>
        <p>For more information on module installation, please visit <a href="https://www.cpan.org/modules/INSTALL.html">the detailed CPAN module installation guide</a>.</p>
      </div>
      <div class="modal-footer">
        <a href="./Parse::RecDescent.html#" data-dismiss="modal" class="btn">Close</a>
      </div>
    </div>
  </div>
</div>
          </main>
          <div class="content-pagination">
          </div>
        </div>
        <footer class="footer">
          <div class="footer-container">
            <div class="footer-social">
              <div class="footer-link footer-logo">
                <a href="https://metacpan.org/">
                  <img src="https://metacpan.org/static/images/metacpan-logo.svg" alt="MetaCPAN" />
                </a>
              </div>
              <a class="footer-social-link" href="https://github.com/metacpan">
                <i class="fab fa-github-square"></i>
              </a>
              <a class="footer-social-link" href="https://fosstodon.org/@metacpan">
                <i class="fab fa-mastodon"></i>
              </a>
            </div>
            <div class="footer-links">
              <div class="footer-link">
                  <a href="https://metacpan.org/about">About</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/about/sponsors">Sponsor</a>
              </div>
              <div class="footer-link">
                  <a href="https://grep.metacpan.org">grep::cpan</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/recent">Recent</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/about/faq">FAQ</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/tools">Tools</a>
              </div>
              <div class="footer-link">
                  <a href="https://fastapi.metacpan.org/">API</a>
              </div>
              <div class="footer-link">
                  <a href="https://www.perl.org/">Perl.org</a>
              </div>
            </div>
            <div class="footer-sponsors">
              <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener">
                <img class="footer-sponsor-bytemark" src="https://metacpan.org/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener">
                <img class="footer-sponsor-liquidweb" src="https://metacpan.org/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener">
                <img class="footer-sponsor-deriv" src="https://metacpan.org/static/images/sponsors/deriv.svg" alt="Deriv logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener">
                <img class="footer-sponsor-geocode" src="https://metacpan.org/static/images/sponsors/geocodelogo.svg" alt="Geocode logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener">
                <img class="footer-sponsor-fastly" src="https://metacpan.org/static/images/sponsors/fastly_logo.svg" alt="Fastly logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener">
                <img class="footer-sponsor-opencage" src="https://metacpan.org/static/images/sponsors/open-cage.svg" alt="OpenCage logo">
              </a>
            </div>
          </div>
        </footer>
        <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Keyboard Shortcuts</h4>
              </div>
              <div class="modal-body row">
<div class="col-md-6">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>Global</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>s</kbd>
        </td>
        <td>Focus search bar</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>?</kbd>
        </td>
        <td>Bring up this help dialog</td>
      </tr>
    </tbody>
  </table>

  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>GitHub</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>p</kbd>
        </td>
        <td>Go to pull requests</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>i</kbd>
        </td>
        <td>go to github issues (only if github is preferred repository)</td>
      </tr>
    </tbody>
  </table>
</div>

<div class="col-md-6">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>POD</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>a</kbd>
        </td>
        <td>Go to author</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>c</kbd>
        </td>
        <td>Go to changes</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>i</kbd>
        </td>
        <td>Go to issues</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>d</kbd>
        </td>
        <td>Go to dist</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>r</kbd>
        </td>
        <td>Go to repository/SCM</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>s</kbd>
        </td>
        <td>Go to source</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>b</kbd>
        </td>
        <td>Go to file browse</td>
      </tr>

    </tbody>
  </table>
</div>

<div class="col-md-12">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>Search terms</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><em>module:</em> (e.g. <a href="https://metacpan.org/search?q=module%3APlugin">module:Plugin</a>)</td>
      </tr>
      <tr>
        <td><em>distribution:</em> (e.g. <a href="https://metacpan.org/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td>
      </tr>
      <tr>
        <td><em>author:</em> (e.g. <a href="https://metacpan.org/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td>
      </tr>
      <tr>
        <td><em>version:</em> (e.g. <a href="https://metacpan.org/search?q=version%3A1.00">version:1.00</a>)</td>
      </tr>
    </tbody>
  </table>
</div>
              </div>
              <div class="modal-footer"></div>
            </div>
          </div>
        </div>
    </body>
</html>
